【问题标题】:cannot get the data from the table of split of one cell by xpath with webdriver无法使用 webdriver 从 xpath 拆分一个单元格的表中获取数据
【发布时间】:2018-08-25 03:02:01
【问题描述】:

无法从一个单元格的拆分表中获取数据,谢谢!

Python 代码

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("url")
print driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[1]/span').text
print driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[1]/i').text
print driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[2]').text

预期结果

5    1-1/2    14.34

错误信息:

Unable to locate element: //html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[1]/span

但实际上这些数据存在于表中

【问题讨论】:

    标签: python selenium-webdriver xpath


    【解决方案1】:

    您的代码是正确的,但您的代码在页面完全加载之前检查元素。 您可以做的最简单的事情是添加wait 条件。

    在下面的代码中,驱动程序轮询 HTML 20 秒以加载要加载的元素。 在检查元素是否存在之前,它并没有完全等待整个 20 秒,而是等待元素加载 20 秒,之后它会抛出一个 NoSuchElementException

    from selenium import webdriver
    
    driver = webdriver.Firefox()
    driver.implicitly_wait(20) # seconds
    
    driver.get("url")
    print(driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[1]/span').text)
    print(driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[1]/i').text)
    print(driver.find_element_by_xpath('//html/body/div/div[3]/table/tbody/tr[1]/td[4]/p[2]').text)
    

    【讨论】:

    • 亲爱的,谢谢!但它显示“消息:缺少'type'参数”
    • 它显示在哪一行?能贴一下异常日志吗?
    • 是的,亲爱的,“selenium.common.exceptions.InvalidArgumentException: Message: Missing 'type' parameter”
    • 嗯,您使用的 Firefox 版本似乎很旧。您可能需要升级到较新的版本。 stackoverflow.com/questions/44254222/…
    • 20秒后元素不可见错误不会抛出,肯定会抛出NoSuchElementExist异常的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    相关资源
    最近更新 更多