Reputation: 39
I am trying to achieve the feature of a Python script using selenium to keep refreshing the current chromepage until, for example, the certain item that makes driver.find_element_by_partial_link_text("Schott")
is found.
I was thinking about this:
while not driver.find_element_by_partial_link_text("Schott"):
driver.refresh
driver.find_element_by_partial_link_text("Schott").click()
However, it seems like the function driver.find_element_by_partial_link_text("Schott")
is not the way to match the need. Is there other way I can achieve this please?
BTW, currently I am using driver.get(url) to open the webpage but I am wondering how do i run the script on existing webpage that I already open?
Upvotes: 0
Views: 4679
Reputation: 2198
Using find_element_...
will raise a NoSuchElementExeception
if it can't find the element. Since I don't know what site you are running this against, i don't know what the best practice would be. However, if it's simply refreshing the page to check for the element, you could try the following:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver.Firefox() # or whatever webdriver you're using
driver.get(url that you are going to)
while True:
try:
driver.find_element_by_partial_link_text("Schott"):
except NoSuchElementException:
driver.refresh
else:
driver.find_element_by_partial_link_text("Schott").click()
break
Upvotes: 1