Python - Selenium webdriver 超时异常错误-2

Python - Selenium webdriver timeout exception error-2

我的目标是从其他网站访问 IMDB 网站并尝试获取电影类型。

这是主页:driver.get("https://sfy.ru/scripts") # main website

NOTE: You might get a "certificate is not valid error". Please set your computer date:07/12/2020

这是我的代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()

driver.get("https://sfy.ru/scripts") #main site

i = 4
driver.find_element_by_xpath("/html/body/div/div[2]/div[1]/p[{}]/a".format(i)).click()

driver.find_element_by_link_text("More info about this movie on IMDb.com").click()


genres = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//h4[.='Genres:']/following-sibling::a")))

aa = (", ".join([genre.text for genre in genres]))

print(aa)

错误:

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
<ipython-input-13-e29dcd0a9a9b> in <module>
     13 
     14 
---> 15 genres = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH, "//h4[.='Genres:']/following-sibling::a")))
     16 
     17 aa = (", ".join([genre.text for genre in genres]))

~\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message: 

使用此代码的目的是:

i = 4
driver.find_element_by_xpath("/html/body/div/div[2]/div[1]/p[{}]/a".format(i)).click()

此页面包含电影列表(driver.get("https://sfy.ru/scripts"))。通过此处的重定向 link (driver.find_element_by_link_text("More info about this movie on IMDb.com").click()),我尝试访问 IMDB 页面并获取每部电影的类型。

所以,我必须创建一个循环来获取所有电影的类型。这就是我尝试使用该代码的原因。

我应该在我的代码中更改什么?

当您单击以获取有关以下内容的更多信息时:

driver.find_element_by_link_text("More info about this movie on IMDb.com").click()

它会在您的Chrome 网页 上打开一个新的windows。所以你的 driver 必须切换 到这个新的 windows。试试这个:

driver.switch_to_window(driver.window_handles[1])

您的其余代码应该可以工作。

而不是 presence_of_all_elements_located() 你必须诱导 for visibility_of_all_elements_located() and you can use either of the following :

  • 使用XPATH:

    print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//h4[.='Genres:']/following-sibling::a")))])
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC