Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Page Refresh if Xpath Not Found

So I recently made a bot for supreme just for fun and to use myself, it is completed the only thing I need to fix is the refresh.

Right now I have an if statement that refreshes the page if item not found. But it only refreshes once. I need help getting it to refresh until it finds the item.

This is the code I have right now for the refresh.

items = driver.find_elements_by_xpath("//article[.//a[contains(.,'MLK')]][.//a[.='Rainbow']]//a")
if items:
items[0].click()
else:
driver.get("http://www.supremenewyork.com/shop/all/sweatshirts")

If anyone knows the syntax for it that would be great, pretty sure I need a while loop but not sure how to set it up.
[Image: UJB3B5t.png]
Like this?

Code
while true:
   time.sleep(3)
   if items:
       items[0].click()
       break
   else:
       driver.get("http://www.supremenewyork.com/shop/all/sweatshirts")

or?

Code
while items[0] != true:
    driver.get("http://www.supremenewyork.com/shop/all/sweatshirts")
    time.sleep(3)
    if items:
        items[0].click()
        break
http://selenium-python.readthedocs.io/waits.html

you could implement these:
Code
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException

timeout_value = 1 # one second
try:
   elements = WebDriverWait(driver, timeout_value).until(lambda driver: driver.find_elements_by_xpath("//article[.//a[contains(.,'MLK')]][.//a[.='Rainbow']]//a"))
except TimeoutException:
   print "Elements were not found in " + str(timeout_value) + " seconds..."
Thanks for all the answers. Helped me out a lot.
[Image: UJB3B5t.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by TriopsFeb 18, 2018 08:47 PM 2 118 Feb 18, 2018 08:47 PM
Last Post: Triops
[.NET] by AjummaFeb 18, 2018 02:57 AM 1 121 Feb 18, 2018 02:57 AM
Last Post: Vyxn
[Shell] by CitrusllJun 7, 2017 02:55 AM 4 173 Jun 7, 2017 02:55 AM
Last Post: Citrusll