0

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?

Edison Hua
  • 39
  • 1
  • 5

1 Answers1

1

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
crookedleaf
  • 2,118
  • 4
  • 16
  • 38
  • Also, is there anyway to open new tab under the same chrome page? like the "Control + T" command and then do some similar operations on the new tab? – Edison Hua Mar 09 '17 at 00:56
  • @EdisonHua what browser are you using? EDIT: nevermind... your tags say chrome. gimme a second – crookedleaf Mar 09 '17 at 01:05
  • to open a new tab in chrome, the only way i was able to get it to work is by using `driver.execute_script('window.open("about:blank", "_blank");')` but then you have to change your focus to that `window_handle` by using `driver.switch_to_window(driver.window_handles[-1])` – crookedleaf Mar 09 '17 at 01:15
  • By window.open, if I wanna open http://www.google.com, what should I do? I tried driver.execute_script('window.open("http://www.google.com");') but failed :( – Edison Hua Mar 09 '17 at 01:31
  • @EdisonHua include the full url, including http://.... `driver.execute_script('window.open("http://www.google.com", "_blank");')` – crookedleaf Mar 09 '17 at 01:34
  • Awesome! Thanks so much! @crookedleaf – Edison Hua Mar 09 '17 at 01:48
  • @EdisonHua no problem. one thing i noticed though while looking at the other questions you ask is that you don't mark questions as answered. it's always good to mark the checkbox on answers provided to your questions because it helps other users of the community find answers to questions they have that are the same or similiar. – crookedleaf Mar 09 '17 at 01:51
  • Got it will do! – Edison Hua Mar 09 '17 at 02:53
  • I have on last question that how do I escape the "" in the execute_script? So if I have a variable : website = "http://www.google.com" and i wanna do : driver.execute_script('window.open(website, "_blank");'), how do i achieve it ? – Edison Hua Mar 09 '17 at 02:54
  • @EdisonHua if you're url is declared as `website`, you can pass it through using string formatting. for example: `driver.execute_script('window.open("%s", "_blank");' % website)` – crookedleaf Mar 09 '17 at 08:19