Hack Forums
[Python] Page Refresh if Xpath Not Found - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Page Refresh if Xpath Not Found (/showthread.php?tid=5825495)



Page Refresh if Xpath Not Found - Clockism - Apr 15, 2018

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.


RE: Page Refresh if Xpath Not Found - Vanagander - Apr 15, 2018

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



RE: Page Refresh if Xpath Not Found - michalmonday - Apr 16, 2018

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..."



RE: Page Refresh if Xpath Not Found - Clockism - Apr 16, 2018

Thanks for all the answers. Helped me out a lot.