【问题标题】:Selenium python trouble finding elementSelenium python 故障查找元素
【发布时间】:2020-04-13 19:49:52
【问题描述】:

我正在尝试访问突出显示的图像,但遇到问题我尝试了 XPATH 和 CSS SELECTOR,但似乎都没有工作。使用 Selenium w python & chrome btw

【问题讨论】:

    标签: python selenium google-chrome


    【解决方案1】:
    from selenium import webdriver
    import os
    cwd = os.getcwd()
    driver = webdriver.Chrome(cwd+'/chromedriver')
    driver.get('site_url')     
    all_spans=driver.find_elements_by_xpath("//span[@class='Practice_Question_Body']")
    for span in all_spans:
        textHtml =span.get_attribute('innerHTML')
    

    cwd是你的目录(显然我不知道chromedriver在哪里)

    【讨论】:

    • 不幸的是没有工作,我不明白当它应该工作时一直给我没有元素异常
    • 如果你把网站的链接发给我,我可以更快地找到问题
    • 相信你能解决这个link
    • 该网站的链接是edgenuity,这是用于在线学习的,所以我不相信你可以访问它
    【解决方案2】:

    试试下面的 xpath :

    wait = WebDriverWait(driver, 10)
    element =wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='Practice_Question_Body'][contains(.,'What is the purpose of')]")))
    

    注意:请在您的解决方案中添加以下导入

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

    【讨论】:

    • 不,它没有工作得到这个错误 [code]Traceback(最近一次调用最后):文件“”,第 1 行,在 元素 = wait.until(EC .element_to_be_clickable((By.XPATH, "//div[@class='Practice_Question_Body'][contains(.,'什么是目的'"))) 文件“C:\Users\Dillionb\AppData\Local\Programs \Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: [/代码]
    猜你喜欢
    • 2018-10-12
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2019-12-11
    • 2016-06-08
    相关资源
    最近更新 更多