0
browser = webdriver.PhantomJS()
def get_score(url):
    browser.get(url)
    elements = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//span[@class='shopdsr-score-con']")))
    description_score = float(elements[0].text)
    service_score = float(elements[1].text)
    logistics_score = float(elements[2].text)

About is my code. Sometimes this may get stuck for quite long time and I expect there should be TimeOutException if it get stuck for 30 seconds because I have this code wait = WebDriverWait(browser, 30) But this never happens, the program just wait there...What's the question?

Andersson
  • 51,635
  • 17
  • 77
  • 129
Shiyu Hu
  • 3
  • 4

2 Answers2

1

As you mentioned, you have set WebDriverWait(browser, 30) so effectively your code will be looking like :

browser = webdriver.PhantomJS()
def get_score(url):
    browser.get(url)
    elements = WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//span[@class='shopdsr-score-con']")))
    description_score = float(elements[0].text)
    service_score = float(elements[1].text)
    logistics_score = float(elements[2].text)   

Logically, there is no error in your code block but the the program just wait there because when you invoke get(url) the web client i.e. PhantomJS Browser doesn't returns back document.readyState = "complete" so early. The JavaScript and the AJAX Calls still keeps loading. Hence Page Loading gets elongated.

Once the document.readyState = "complete" is returned by the web client i.e. PhantomJS Browser then only Selenium executes the next line of code:

elements = WebDriverWait(browser, 30).until(EC.presence_of_all_elements_located((By.XPATH, "//span[@class='shopdsr-score-con']")))

Hence the program just wait there for some more time.

Update :

As per your comment you need to look at the options of pageLoadStrategy either setting pageLoadStrategy to eager or none as per this QA Don't wait for a page to load using Selenium in Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Updated my Answer. Please mark the Answer as Accepted by clicking on the tick mark beside my Answer just below the VoteDown arrow so the tick mark turns green. – undetected Selenium Dec 04 '17 at 12:21
0

use try,except

   from selenium.common.exceptions import TimeoutException
   from selenium.webdriver.support.ui import WebDriverWait
   from selenium.webdriver.support import expected_conditions as EC

   timeout=30

   try:
       element_present1 = EC.presence_of_element_located((By.XPATH, "//input[@name='username']"))
       WebDriverWait(driver, timeout).until(element_present1)
   except TimeoutException:
       print "Timed out waiting for login page to load"
pankaj mishra
  • 2,555
  • 2
  • 17
  • 31