首页 > 解决方案 > Selenium xpath Webelement问题

问题描述

我对硒有点陌生,所以请原谅我的无知。我正在尝试从网站获取 ajax 加载表的行。代码看起来像这样:

try:
    table = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "tableid")))

finally :
    
    for row in table.xpath(".//tr"):
        print(row)

但是,我收到以下错误:

AttributeError: 'WebElement' object has no attribute 'xpath'

让我知道错误是什么

标签: python-3.xseleniumselenium-chromedriver

解决方案


此错误消息...

AttributeError: 'WebElement' object has no attribute 'xpath'

...意味着您的程序已尝试调用不可xpath用的属性。


解决方案

WebElement没有属性为xpath. 相反,您可能喜欢调用find_element_by_xpath()方法。因此,您必须有效地更改线路:

for row in table.xpath(".//tr"):

和:

for row in table.find_elements_by_xpath("./tr"):

推荐阅读