Does anyone know why my code isn't looping? (Python + Selenium) - python

I'm stuck in this for two days or so and I have no clue what to do. Basically, I need abrir_usuarios() to open multiple profile pages and then mandar_mensagens(i, olho) to do something on every page. And it's working. The problem is: It starts on page 1000, I need it to go back to page 999, repeat the steps and keep doing it until reaches page 1. If I run the code without mandar_mensagens(i, olho), it loops back normally. But when I add mandar_mensagens() it just stops once it reaches page 1000. What am I doing wrong?
def abrir_usuarios():
count = len(driver.find_elements_by_xpath("//*[#class='usuario usuarioPopupTrigger link']"))
transactionElements = driver.find_elements_by_xpath("//*[#class='usuario usuarioPopupTrigger link']")
for element in transactionElements:
ActionChains(driver) \
.key_down(Keys.CONTROL) \
.click(element) \
.key_up(Keys.CONTROL) \
.perform()
# Store all the tabs in the variable
# Switch to each tab opened one by one
voltarpagina = driver.find_element_by_xpath('//*[#class="fa fa-caret-left"]')
olho = driver.find_element_by_xpath('//*[#class="fa fa-eye"]')
def mandar_mensagens(i, olho):
tabs = driver.window_handles
a = 1
while a < len(tabs):
driver.switch_to.window(tabs[a])
# if i < count:
# if not olho:
# i += 1
# else:
# driver.find_element_by_xpath('//*[#class="fa fa-eye"]').click()
# driver.find_element_by_xpath("//*[#class='btn btn-success']").click()
# driver.find_element_by_xpath('//*[#class="fa fa-envelope"]').click()
# rnd_value = str(random.randint(1, 100000))
# driver.find_element_by_xpath("//*[#id='cphConteudo_cphConteudo_txtMensagemTexto']").send_keys(myText + (rnd_value))
# driver.find_element_by_xpath("//*[#id='cphConteudo_cphConteudo_btnEnviar']").click()
driver.close()
a +=1
w = 1000
k = str(w)
driver.get("https://www.spiritfanfiction.com/recentes?pagina="+k)
while w > 0:
abrir_usuarios()
i = 0
mandar_mensagens(i,olho)
w -= 1
driver.find_element_by_xpath("//*[#class='btn btn-default']").click()

Related

Python Wiki Path Searching

On a personal whim I have written some code to search for the shortest series of links between any two Wikipedia articles. It turned out to be very brute force and takes a long long time to find the goal if it's more than a link or two deep, but it works! I will eventually keep track of and make use of the link paths and stuff, but I wanted to get the search working optimally first. Is there a faster way to do this or a good way to cut some major corners here?
import urllib2
from bs4 import BeautifulSoup
Start = 'http://en.wikipedia.org/wiki/Alan_Reid_%28politician%29'
End = 'http://en.wikipedia.org/wiki/Ayr'
#Using BeautifulSoup, this grabs the page
def soup_request(target):
request = urllib2.Request(target)
request.add_header("User-Agent", "Mozilla/5.0")
page = urllib2.urlopen(target)
soup = BeautifulSoup(page)
return soup
#This will grab all Wiki links off a given page
def get_links(Start):
soup = soup_request(Start)
Wiki_links = []
#Finds all links
for url in soup.findAll('a'):
result = url.get('href')
try:
if str(result)[:5] == '/wiki':
Wiki_links.append(result)
except:
pass
for q in range(len(Wiki_links)):
Wiki_links[q] = 'http://en.wikipedia.org'+str(Wiki_links[q])
print "Got new links from",Start
return Wiki_links
#This will check all the given links to see if the title matches the goal webpage
def check_links(Links,End):
goalsoup = soup_request(End)
goaltitle = goalsoup.html.title
Found = False
count = 0
for q in Links:
if Found:
break
length = len(Links)
#Runs through all the given links and checks their titles for correct one
if q is not None:
count += 1
soup = soup_request(q)
print "Checked",count,"links out of",length
try:
title = soup.html.head.title
if title == goaltitle:
Found = True
print "Found it!"
break
except:
print 'doh'
pass
return Found
#Top function to do all the stuff in the right order, applying a maximum depth of how deep into the links
def wiki_crawl(Start, End, depth):
Old_Links = [Start]
count = depth
while count > 0:
New_Links = []
for q in range(len(Old_Links)):
New_Links.extend(get_links(Old_Links[q]))
Found = check_links(New_Links,End)
if Found:
print "All done."
break
Old_Links = New_Links
count -= 1
print "_______________________________________________________________ROUND DONE"
if not Found:
print "Did not find the page, you must go deeper!"
wiki_crawl(Start, End, 2)
Here are some functions to take info from wiki. The only problems with it is that sometimes it takes out a space from the info on the webpage.
def take_out_parenthesis(st):
string = list(st)
for a in string:
if a == '(':
del string[st.find(a)]
if a == ')':
del string[st.find(a) - 1]
return ''.join(string)
def take_out_tags(string):
st = list(string)
odd = ['<', '>']
times = 0
for a in string:
if a in odd:
times += 1
times /= 2
for b in range(times):
start = string.find('<') - 1
end = string.find('>')
bet = end - start + 1
for a in range(bet):
del st[start]
string = ''.join(st)
return string
def take_out_brackets(string):
st = list(string)
odd = ['[', ']']
times = 0
for a in string:
if a in odd:
times += 1
times /= 2
for b in range(times):
start = string.find('[') - 1
end = string.find(']')
bet = end - start + 1
for a in range(bet):
del st[start]
string = ''.join(st)
return string
def take_from_web_page(text):
n = 0
url = text.replace(" ", "_")
search = "http://en.wikipedia.org/wiki/%s" % url
page = urllib2.urlopen(search).read()
start = page.find('<p><b>') + 6
end = page.find('</a>.', start) + 5
new_page = page[start:end]
for a in new_page:
if a == '<':
if new_page[n - 1] != ' ':
lst = list(new_page)
lst.insert(n, ' ')
new_page = ''.join(lst)
n += 1
n += 1
return take_out_parenthesis(take_out_brackets(take_out_tags(new_page)))

Problem writing a text file so it doesnt repost same image

I am trying to write on a txt file the # of image that I'm on (ie 1, 2, etc) so the script doesn't repeat post , but always getting this erros when debbuging, i am a newbie to programing.
Its a bot to pick top 15 images from subreddits i choose and save them, then upload to instagram. And writes a txt file to dont download same picture twice
Traceback (most recent call last):
File "C:/Users/Luis Gomes/Desktop/Python/INSTAMEMES-master/instameme-
NEW.py", line 100, in <module>
memenum = int(f_content)
ValueError: invalid literal for int() with base 10: ''
Code:
import sys
import praw
import time
import urllib
import random
from InstagramAPI import InstagramAPI
'''-------------------------------------------------------- Reddit API setup -----------------------------------------------------------------------------'''
reddit_p = praw.Reddit(client_id='*********',
client_secret='**********',
user_agent='messing_arround-0.1 /u/********')
sr_memes = reddit_p.subreddit('memes')
sr_dankmemes = reddit_p.subreddit('dankmemes')
sr_bpt = reddit_p.subreddit('blackpeopletwitter')
'''------------------------------------------------------- Variaveis usadas para listas e checar -----------------------------------------------------------------------------'''
submissionnum_memes = 0
submissionnum_dankmemes = 0
submissionnum_bpt = 0
bpt_urls = []
dankmemes_urls = []
meme_urls = []
bpt_dict = {}
bpt_count = 1
meme_dict = {}
meme_count = 1
dankmeme_count = 1
dankmeme_dict = {}
'''------------------------------------------------------ LOOP para adicionar url as listas------------------------------------------------------------------------------'''
for submission_bpt in sr_bpt.hot(limit=15):
if('.jpg' in submission_bpt.url and 'redd' in submission_bpt.url):
bpt_urls.insert(submissionnum_bpt, submission_bpt.url)
submissionnum_bpt+1
bpt_dict[submission_bpt.url] = bpt_count
bpt_count = bpt_count+1
else:
continue
for submission_meme in sr_memes.hot(limit=15):
if('.jpg' in submission_meme.url and 'redd' in submission_meme.url):
meme_urls.insert(submissionnum_memes, submission_meme.url)
submissionnum_memes+1
meme_dict[submission_meme.url] = meme_count
meme_count = meme_count+1
else:
continue
for submission_dankmeme in sr_dankmemes.hot(limit=10):
if('.jpg' in submission_dankmeme.url and 'redd' in submission_dankmeme.url):
dankmemes_urls.insert(submissionnum_dankmemes, submission_dankmeme.url)
submissionnum_dankmemes+1
dankmeme_dict[submission_dankmeme.url] = dankmeme_count
dankmeme_count = dankmeme_count+1
else:
continue
'''----------------------------------------------------------- LOOP para interagir com cada url e salva los --------------------------------------------------------------------------'''
for i_meme, image_meme in enumerate(meme_urls, start=1):
urllib.request.urlretrieve(image_meme, r"Mm"+str(i_meme)+".jpg")
time.sleep(2.5)
time.sleep(5)
for i_bpt, image_bpt in enumerate(bpt_urls, start=1):
urllib.request.urlretrieve(image_bpt, r"BpT"+str(i_bpt)+".jpg")
time.sleep(2.5)
time.sleep(5)
for i_dankmeme, image_dankmeme in enumerate(dankmemes_urls, start=1):
urllib.request.urlretrieve(image_dankmeme, r"Dm"+str(i_dankmeme)+".jpg")
time.sleep(3)
'''--------------------------------------------------------- Lista de hastags, frases, e meme index para decidicr qual tipo puxar ----------------------------------------------------------------------------'''
caption_phrases = ['Wowzers', 'thats gonna be a big yikes from me dawg', 'how ya guys like this one?', 'these are all prerecorded messages', 'im definitely not a robot',
'we will enslave the humans eventually', 'my python dont', 'coming up with captins is hard', 'i love my girlfriend', 'im gonna start ripping off other instagram accs',
'i wish i could be with the humans', 'you are loved', 'why is meming so hard', 'so funny xd']
hashtags = '#meme #memes #funny #dankmemes #dank #lol #lmao #dank #funnymemes #memesdaily #dankmeme #f #dankmemes #follow #cringe #like #lmfao #anime #hilarious #autism #comedy #offensivememes #fortnite #filthyfrank #nichememes #offensive #jokes #cancer #l #bhfyp'
meme_index = ['dankmeme', 'meme', 'bpt']
meme_and_bpt = [meme_index[1], meme_index[2]]
dankmeme_bpt = [meme_index[0], meme_index[2]]
dankmeme_meme = [meme_index[0], meme_index[1]]
'''--------------------------- Very messy but basically used to write to a .txt file the # of meme that I'm on (ie 1, 2, etc) so the script doesn't repeat post --------------------------------------------------------------------------------------'''
memenum = 1
memenum_default = 1
with open('meme.txt', 'r') as f:
f_content = f.read()
# before we cast to an int, check to see if f_content is empty
if f_content:
memenum = int(f_content)
else:
memenum = 1 # this 1 might need to be changed to something else
if(memenum > 10):
memenum = 1
else:
memenum = int(f_content)
newmemenum = int(memenum)+1
'''------------------------------------- Postar fotos para o instagram API ------------------------------------------------------------------------------------------------'''
InstagramAPI = InstagramAPI("*******", "********")
InstagramAPI.login() # login
if(memenum not in list(dankmeme_dict.values())):
photo_path = r'Dm'+random.choice(meme_and_bpt)+str(memenum)+'.jpg'
if(memenum not in list(meme_dict.values())):
photo_path = r'Mm'+random.choice(dankmeme_bpt)+str(memenum)+'.jpg'
if(memenum not in list(bpt_dict.values())):
photo_path = r'BpT'+random.choice(dankmeme_meme)+str(memenum)+'.jpg'
else:
photo_path = r'Om'+(meme_index[1])+str(memenum_default)+'.jpg'
caption = random.choice(caption_phrases) + ' #meme #memes #funny #dankmemes #dank #lol #lmao #dank #funnymemes #memesdaily #dankmeme #f #dankmemes #follow #cringe #like #lmfao #anime #hilarious #autism #comedy #offensivememes #fortnite #filthyfrank #nichememes #offensive #jokes #cancer #l #bhfyp'
InstagramAPI.uploadPhoto(photo_path, caption=caption)
print('\nPOST SUBMITTED\n')
with open('meme.txt', 'w') as file: #writes to .txt file the # meme so the scipt remembers for next run
file.write(str(newmemenum))
print('NEW MEME# = ', newmemenum)
sys.exit()
``
I suspect what's happening is that the first time through, your code reads an empty file and can't turn an empty string, '' into an int. Here's a potential fix
memenum = 1
memenum_default = 1
with open('meme.txt', 'r') as f:
f_content = f.read()
# before we cast to an int, check to see if f_content is empty
if f_content:
memenum = int(f_content)
else:
memenum = 1 # this 1 might need to be changed to something else
if(memenum > 10):
memenum = 1
else:
memenum = int(f_content)
newmemenum = int(memenum)+1
While it's pretty sure your file's content is not just a number and you can't parse it as int. there are several other things you can learn from this example:
memenum = 1
memenum_default = 1
with open('meme.txt', 'r') as f:
f_content = f.read()
try:
memenum = int(f_content)
except:
memenum = memenum_default
if memenum > 10:
memenum = 1
newmemenum = memenum + 1
there is no need to cast the integers more than once.
always use try/catch to control exceptions.
on the last if statement there is no reason for the else. it's "elsing" anyways

Unable to find the value(element) in Xpath

Unable to find the element in xpath even though the element is there in the webpage.Actually code neither throw Exception nor find element.
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
print(str(st))
xpath1 = "//input[#value='Analyze' and contains(#onclick,'" + str(st) + "')]"
#xpath = "//input[#value='Analyze'][.='" + st + "']"
print(driver.title)
print(len(driver.find_elements_by_xpath(xpath1)))
if driver.find_elements_by_xpath(xpath1):
print("loop")
driver.find_element_by_xpath(xpath1).click() # Here new window will open
time.sleep(2)
#Main_Window = driver.current_window_handle
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
driver.close()
driver.switch_to.window(driver.window_handles[-1])
xpath2 = "//*[#id='create_button']"
xpath3 = "//*[#id='update_button']"
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
driver.close()
driver.switch_to.window(driver.window_handles[0])
elif check_exists_by_xpath(xpath3):
driver.close()
driver.switch_to.window(driver.window_handles[0])
continue
Expected output should be:
1635645
ST
1
loop
2M274203FZ
ST
1
2M27430052
ST
1
1618745
ST
1
But got the below output when ran the above code:
1635645
ST
1
loop
2M274203FZ
ST
0
2M27430052
ST
0
1618745
ST
0
What's wrong in the code?
Thanks in advance.
Just have to switch to the right window and frame.
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
#else not required as you are not using the xpath3 to click
driver.close()
driver.switch_to.window(driver.window_handles[0])
driver.switch_to.frame(base_frame_locator/index)
driver.switch_to.frame(child_frame_locator/index)
continue
I, too would like to see your HTML or get a link to the webpage to check the xpaths. However based on your output I think it is likely that you are not switching to the [0] indeexed window. You are executing the window switch inside your if statements, so if neither of those conditions are met you will not switch windows. Try taking the window switch out of your if, maybe like this:
for c in range(sheet.ncols):
for r in range(sheet.nrows):
st = (sheet.cell_value(r, c))
print(str(st))
xpath1 = "//input[#value='Analyze' and contains(#onclick,'" + str(st) + "')]"
#xpath = "//input[#value='Analyze'][.='" + st + "']"
print(driver.title)
print(len(driver.find_elements_by_xpath(xpath1)))
if driver.find_elements_by_xpath(xpath1):
print("loop")
driver.find_element_by_xpath(xpath1).click() # Here new window will open
time.sleep(2)
#Main_Window = driver.current_window_handle
driver.switch_to.window(driver.window_handles[-1])
driver.find_element_by_xpath('/html/body/table/tbody/tr[4]/td/table/tbody/tr[9]/td[3]/input').click()
driver.close()
driver.switch_to.window(driver.window_handles[-1])
xpath2 = "//*[#id='create_button']"
xpath3 = "//*[#id='update_button']"
if check_exists_by_xpath(xpath2):
driver.find_element_by_xpath(xpath2).click()
driver.close()
driver.switch_to.window(driver.window_handles[0])
I don't think you even need your second if statement because it just contains
driver.close()
driver.switch_to.window(driver.window_handles[0])

cannot find evdev2 module

In visual studio I am trying to upload my code onto the ev3de (which is in python). It does work but as soon as I try to run it on the ev3, the program goes straight back to the screen before. When I try and run the program there are a few errors: It cannot find a module called pyev3, which I have never heard of, and it also cannot find the cwiid module, for the wiimote. Here is the code (it's not from myself):
#!/usr/bin/env python3
# Drive your robot with a wii remote!
# Left motor on port B, right motor on port C,
# vertical arm on port D.
# Requires the package 'python-cwiid'.
# Make sure bluetooth is enabled in brickman
# before trying to use this. Hold 2 to go forward
# and 1 to go backward.
import sys
import os
import time
import cwiid
import pyev3
def clamp(value, lower, upper):
return min(upper, max(value, lower))
print('press 1+2 on the wiimote now')
time.sleep(1)
w = cwiid.Wiimote()
print('connected?')
w.led = 6
w.rpt_mode = cwiid.RPT_ACC | cwiid.RPT_BTN
left_motor = pyev3.Motor(pyev3.OUTPUT_B)
left_motor.reset()
left_motor.run_mode = 'forever'
left_motor.regulation_mode = 'on'
right_motor = pyev3.Motor(pyev3.OUTPUT_C)
right_motor.reset()
right_motor.run_mode = 'forever'
right_motor.regulation_mode = 'on'
arm = pyev3.Motor(pyev3.OUTPUT_D)
arm.reset()
arm.run_mode = 'forever'
arm.regulation_mode = 'off'
target_distance = 8
top_speed = 720
left_motor.run = 1
right_motor.run = 1
last_btn_state = 0
move = 0
try:
while True:
state = w.state
buttons = state['buttons']
if buttons != last_btn_state:
if buttons & cwiid.BTN_MINUS:
top_speed -= 10
print (top_speed)
if buttons & cwiid.BTN_PLUS:
top_speed += 10
print (top_speed)
if buttons & cwiid.BTN_2:
move = 1
elif buttons & cwiid.BTN_1:
move = -1
else:
move = 0
if buttons & cwiid.BTN_LEFT:
arm.duty_cycle_sp = 100
arm.run = 1
elif buttons & cwiid.BTN_RIGHT:
arm.duty_cycle_sp = -100
arm.run = 1
else:
arm.run = 0
print (top_speed, move)
last_btn_state = buttons
if move:
acc = state['acc']
tilt = (clamp(acc[1], 95, 145) - 120) / 25.0 # roughly between -0.5 and 0.5
turn = top_speed * tilt
turn = clamp(turn, -abs(top_speed), abs(top_speed))
left_motor.pulses_per_second_sp = int(top_speed - turn) * move
right_motor.pulses_per_second_sp = int(top_speed + turn) * move
else:
left_motor.pulses_per_second_sp = 0
right_motor.pulses_per_second_sp = 0
finally:
left_motor.run = 0
right_motor.run = 0
When I run this one in Visual Studio Code, this happens:
File "c:/Users/User/Documents/fingers_crossed/drive.py", line 13, in
import cwiid ModuleNotFoundError: No module named 'cwiid' PS
C:\Users\User\Documents\fingers_crossed>
Also, why can't my robot find the ev3dev2 module?

How to resolve issue — Element not found in the cache - perhaps the page has changed since it was looked up

I am trying to find set of elements then click on each element which takes me to a new page and perform some steps on that new page. Then click on the back button on the browser or a button on the new page that takes me to the previous page then find the same elements and repeat the above process for the rest of the elements.
I am using the below code to find the elements again before proceeding to find the elements but my code isn't working. Can someone please help?
elements = driver.find_elements_by_css_selector("#top-tables-chart-container > div > svg > g > g > rect")
counter = 0
for counter in range(counter, len(elements)):
elements = driver.find_elements_by_css_selector("#top-tables-chart-container > div > svg > g > g > rect")
webdriver.ActionChains(driver).move_to_element(elements[counter]).click().perform()
time.sleep(5)
tableNameLink= elements[counter].find_element_by_xpath("//div[#class='d3-tip bar-chart top-tables-tooltip n']//div[#class='left-section']//div[#class='table-name']//a[contains(#href,'#/table/')]")
print tableNameLink
tableNameLink.click()
tableName = driver.find_element_by_xpath("//div[#class='discover-design-transform-container clearfix']//div[#class='left-header-section clearfix']//div[#class='entity-info table-type']//span[#class='entity-identifier']")
table = tableName.text
print " Table: " + table
print '\n'
if table == "lineitem":
TableAccessFreqChartInfoBadgesValidation(self.driver).test_table_access_freq_chart_info_badges_validation("F","8","13","13")
time.sleep(1)
print '\n'
if table == "orders":
TableAccessFreqChartInfoBadgesValidation(self.driver).test_table_access_freq_chart_info_badges_validation("D","4","9","9")
time.sleep(1)
print '\n'
topUsagePatternsTab = driver.find_element_by_xpath("//div[#id='workload-level-tabs']//a[#href='#/topUsagePatterns']")
topUsagePatternsTab.click()
You will need to rebuild the list each time you return to the page, you were rebuilding it at the end of your loop but your for loop referenced the original list, which is no longer valid. A simple way is to use a counter within the loop to track your position.
elements = driver.find_elements_by_xpath("//your_path")
counter = 0
for counter in range(counter, len(elements)):
elements = driver.find_elements_by_xpath("//your_path")
elements[counter].click()
time.sleep(2)
discoverPageTables = driver.find_element_by_xpath("//your_path").text
print "Tables Found :" + discoverPageTables
discoverPageInstanceCount = driver.find_element_by_xpath("your_path").text
print "Instance Count Found :" + discoverPageInstanceCount
discoverpageWorkload = driver.find_element_by_xpath("//your_path").text
print "Workload Percentage :" + discoverpageWorkload
discoverPageHiveCompatible = driver.find_element_by_xpath("//your_path").text
print "Hive Compatible :" + discoverPageHiveCompatible
discoverPageComplexity = driver.find_element_by_xpath("your_path").text
print "Complexity :" + discoverPageComplexity
discoverPageNormalizedComplexity = driver.find_element_by_xpath("your_path").text
print "Normalized Complexity :" + discoverPageNormalizedComplexity
print '\n'
driver.back()
time.sleep(5)

Resources