selenium 引发超时异常(消息、屏幕、堆栈跟踪)

0md85ypi  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(324)

我对python和selenium都很陌生,我正试图基于它做一个项目。代码示例:

options = Options()
options.headless = True
service_ = Service(executable_path = 'C:/Users/Downloads/chromedriver_win32/chromedriver.exe')
driver = webdriver.Chrome(service = service_, options = options)
wait = WebDriverWait(driver, 30)
driver.get(url_2)
manga_1 = driver.find_elements(By.XPATH, "//div[@class = 'item-summary']")
manga_1 = wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class = 'item-summary']")))

当我试图运行代码时,它给了我一个错误:

raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
Backtrace:
    Ordinal0 [0x00ABACD3+2075859]
    Ordinal0 [0x00A4EE61+1633889]
    Ordinal0 [0x0094B7BD+571325]
    Ordinal0 [0x0097AC2F+764975]
    Ordinal0 [0x0097AE1B+765467]
    Ordinal0 [0x009AD0F2+970994]
    Ordinal0 [0x00997364+881508]
    Ordinal0 [0x009AB56A+963946]
    Ordinal0 [0x00997136+880950]
    Ordinal0 [0x0096FEFD+720637]
    Ordinal0 [0x00970F3F+724799]
    GetHandleVerifier [0x00D6EED2+2769538]
    GetHandleVerifier [0x00D60D95+2711877]
    GetHandleVerifier [0x00B4A03A+521194]
    GetHandleVerifier [0x00B48DA0+516432]
    Ordinal0 [0x00A5682C+1665068]
    Ordinal0 [0x00A5B128+1683752]
    Ordinal0 [0x00A5B215+1683989]
    Ordinal0 [0x00A66484+1729668]
    BaseThreadInitThunk [0x76497BA9+25]
    RtlInitializeExceptionChain [0x778EBB9B+107]
    RtlClearBits [0x778EBB1F+191]

Process finished with exit code 1

我试着通过看youtube来修复它,但我被卡住了,不知道它会是什么。

sg3maiej

sg3maiej1#

webpage中有许多项目摘要。
要从***项目摘要***中提取文本,您必须为visibility_of_all_elements_located()导出WebDriverWait,并使用List Comprehension,您可以使用以下locator strategies之一:

  • 使用 *CSS选择器 *:
driver.get("https://1stkissmanga.io/")
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.item-summary")))])
driver.quit()
  • 使用 XPATH
driver.get("https://1stkissmanga.io/")
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='item-summary']")))])
driver.quit()
  • 控制台输出:
['Martial Peak\n3.2\nChapter 2829\nChapter 2828', 'Tyrant Daddy’s Petite Bag\n5\nChapter 39\nChapter 38', 'If I Die, I will Be Invincible\n4.3\nChapter 39\nChapter 38', 'Be Jealous\n4\nChapter 24\nChapter 23', 'Deal With A Bad Boy\n5\nChapter 23\nChapter 22', 'I’m a Villainess But I Became a Mother\n4.1\nChapter 25.5\nChapter 25', 'Sunset Boulevard\n4\nChapter 20\nChapter 19', 'Elena Evoy Observation Diary\n4.5\nChapter 67\nChapter 66', 'Her Peculiar Visitor\n4.5\nChapter 59\nChapter 58 November 25, 2022', 'The Ex\n4.1\nChapter 117\nChapter 116', 'Love Hug\n3.5\nChapter 49\nChapter 48', 'Liar\n3.4\nChapter 43\nChapter 42', 'Usami’s Little Secret!\n4\nChapter 24\nChapter 23', 'Revenge Against the Immoral\n3.4\nChapter 60\nChapter 59', 'The Wounded Devil\n2.8\nChapter 23\nChapter 22', 'The Double Life of a Daydreaming Actress\n3.3\nChapter 62\nChapter 61', 'HOT\nDestined to be Empress\n3.7\nChapter 80\nChapter 79', 'The Widowed Empress Needs Her Romance\n4.4\nChapter 58\nChapter 57', 'Brother Mode\n3.6\nChapter 75\nChapter 74', 'Super Gene\n3.8\nChapter 83\nChapter 82']

*注意:必须添加以下导入:

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

相关问题