keywords = ['no stock','out of stock','not available']
n = 0
while True:
n+=1
print(f'now check {n} times')
for keyword in keywords:
if keyword in driver.page_source:
print(f'found {keyword}, refresh after 30 seconds')
time.sleep(30)
driver.get(url)
else:
print(f'could not find any of keyword')
break
Hi guys, hope you are doing well.
I am trying to use selenium and to check whether an item is available in a webpage. My idea is to put all keywords related to no stock in a list and loop every 30 seconds to check.
however, if I put a break on it, the code only scan the 1st item in list. Is there any way that to break the loop if all the three keywords are not in website?
Thanks for your help.
Maybe is this the logic you are looking for?
keywords = ['no stock','out of stock','not available']
n = 0
while True:
n+=1
print(f'now check {n} times')
keywords_found = 0
for keyword in keywords:
if keyword in driver.page_source:
keywords_found += 1
print(f'found {keyword}, {keywords_found} of {len(keywords)}')
if keywords_found == len(keywords):
print(f"We found the {len(keywords)} items of the list intot he page, so sleep and navigate")
time.sleep(30)
driver.get(url)
else:
print(f'could not the {len(keywords)} keywords, break')
break
Related
I made a code to scrape some website. A list of IDs is iterated in the website, and it contains two conditions(If and Elif). But the problem is with the Elif. The error is it doesn't found the elif element (elem2).
I read in this question Python if elif else can't go to the elif statement Selenium the solution is a try/except, butI already used a Try/except to make works the if statement. What is a solution to make this code works with two conditions?
The code looks like this:
for item in list:
input = driver.find_element(By.ID, "busquedaRucId")
input.send_keys(item)
time.sleep(2)
elem1 = driver.find_element(By.ID, 'elem1')
elem1_displayed = elem1.is_displayed()
elem2 = driver.find_element(By.ID, 'elem2')
elem2_displayed = elem2.is_displayed()
try:
if elem1_displayed is True:
code to scrape given de first condition
elif elem2_displayed is True:
code to scrape given de second condition
except NoSuchElementException:
input = driver.find_element(By.ID, ('busquedaRucId')).clear()
Than you for any help. I'm stuck with this problem for two weeks.
I would restructure your code by wrapping the find_element function in a function which handles NoSuchElementExceptions by returning False, basically making the error silent:
def element_exists_and_displayed(driver, id):
try:
return driver.find_element(By.ID, id).is_displayed()
except NoSuchElementException:
return False
for item in list:
input = driver.find_element(By.ID, "busquedaRucId")
input.send_keys(item)
time.sleep(2)
if element_exists_and_displayed(driver, 'elem1'):
# code to scrape given first condition
pass
elif element_exists_and_displayed(driver, 'elem2'):
# code to scrape given second condition
pass
else:
driver.find_element(By.ID, ('busquedaRucId')).clear()
while True:
try:
element = driver.find_element(By.XPATH, "//*[contains(#href,'dawson')]")
element.click()
break
except NoSuchElementException:
driver.refresh()
time.sleep(3)
Above is the try and except block that looks for a word in a Href and if it contains it the element is clicked. I wish to go through multiple of these given words and try them. So if the first word is not found it then goes on to the next word. It does not matter if it refreshes in between I just want it to iterate through these words and if it finds one it will click. How can I add more words into the try block?
Any help would be great.
Thank you
Search for an element in separate loop
def find_link_by_word_in_href(driver, words):
for word in words:
try:
return driver.find_element(By.XPATH, f"//*[contains(#href,'{word}')]")
except NoSuchElementException:
pass
while True:
element = find_link_by_word_in_href(driver, ['dawson', 'denbigh', 'and_so_on'])
if element is not None:
element.click()
break
else:
driver.refresh()
time.sleep(3)
I'd like to use the find_between function to retrieve index-able values from a specific web server.
I'm using the requests module to gather some source code from a specific website seen on line 18:
response = requests.get("https://www.shodan.io/search?query=Server%3A+SQ-WEBCAM")
and I'd like to call the find_between function to retrieve all the values (all items on page each item represented by the incrementing value of 'n') with the specified find_between parameters:
x = find_between(response.content,'/></a><a href="/host/','">---')
Anyone know how to pull this off?
import sys
import requests
from time import sleep
# Find between page tags on page.
def find_between( s, tag1, tag2 ):
try:
start = s.index( tag1 ) + len( tag1 )
end = s.index( tag2, start )
return s[start:end]
except ValueError:
return ""
def main():
# Default value for 'n' index value (item on page) is 0
n = 0
# Enter the command 'go' to start
cmd = raw_input("Enter Command: ")
if cmd == "go":
print "go!"
# Go to this page for page item gathering.
response = requests.get("https://www.shodan.io/search?query=Server%3A+SQ-WEBCAM")
# Initial source output...
print response.content
# Find between value of 'x' sources between two tags
x = find_between(response.content,'/></a><a href="/host/','">---')
while(True):
# Wait one second before continuing...
sleep(1)
n = n + 1
# Display find_between data in 'x'
print "\nindex: %s\n\n%s\n" % (n, x)
# Enter 'exit' to exit script
if cmd == "exit":
sys.exit()
# Recursive function call
while(True):
main()
A few things in your code appear to need addressing:
The value of x is set outside (before) your while loop, so the loop increments the index n but prints the same text over and over because x never changes.
find_between() returns only a single match, and you want all matches.
Your while loop never ends.
Suggestions:
Put the call to find_between() inside the while loop.
Each successive time you call find_between(), pass it only the portion of the text following the previous match.
Exit the while loop when find_between() finds no match.
Something like this:
text_to_search = response.content
while(True):
# Find between value of 'x' sources between two tags
x = find_between(text_to_search, '/></a><a href="/host/', '">---')
if not x:
break
# Wait one second before continuing...
sleep(1)
# Increment 'n' for index value of item on page
n = n + 1
# Display find_between data in 'x'
print "\nindex: %s\n\n%s\n" % (n, x)
# Remove text already searched
found_text_pos = text_to_search.index(x) + len(x)
text_to_search = text_to_search[found_text_pos:]
how repeat code again again again this every work
I want the code below to always work and it should be repeated, and again this function should be repeated and not removed from the program.
def ref(self):
driver = self.driver
nextB2 = driver.find_element_by_xpath("""//section/span/button/span[#aria-label='Like']""")
nextB2.click()
time.sleep(5)
nextB3 = driver.find_element_by_xpath("""//section/span/button/span[#aria-label='Like']""")
nextB3.click()
time.sleep(6)
nextB4 = driver.find_element_by_xpath("""//section/span/button/span[#aria-label='Like']""")
nextB4.click()
time.sleep(7)
driver.refresh()
time.sleep(5)
driver.switch_to_frame('ref')
driver.refresh('ref')
you can use for loop with range to stop at perticular count like
for i in range(10): #10 times
ref() #function call
if you want it to run for ever
while True: #loop that never stops
ref()
you can use break and continue for conditional breaks
while True:
if foo == foo:
break #break or stop the while oop
elif foo == bar:
continue #skip current iteration and continue execution
else:
ref()
I want to know if there is any way to log the number of times my page has refreshed in command prompt when running.
want it to tell me the number of times it has refreshed. Refresh is located between while true: and continue. thanks
driver = webdriver.Chrome(chrome_path)
driver.get(link)
while True:
size = driver.find_elements_by_xpath(".//*[#id='atg_store_picker']/div/div[2]/div[1]/div[1]/span[2]/a[4]")
if len(size) <= 0:
time.sleep(0.5)
print "PAGE NOT LIVE"
driver.refresh()
continue
else:`enter code here`
print 'LIVE!!!!'
break
the answer to my question was very simple...
driver = webdriver.Chrome(chrome_path)
driver.get(link)
count = 0
while True:
size = driver.find_elements_by_xpath(".//*[#id='atg_store_picker']/div/div[2]/div[1]/div[1]/span[2]/a[4]")
if len(size) <= 0:
count +=1
print 'Refresh Count:', count
time.sleep(2)
driver.refresh()
continue
else:
print 'LIVE!!!!'
break