【问题标题】:Why is my Selenium xpath expression returning an [object attribute] instead of an element?为什么我的 Selenium xpath 表达式返回 [object attribute] 而不是元素?
【发布时间】:2019-11-09 21:23:51
【问题描述】:

我正在制作一个刮板,它将通过我的网页并抓取所有链接。许多链接都在封闭列表中,也称为树。因此,我找到了包含所有链接的 xpath。我在 google inspect 中运行了以下 xpath,它运行得非常好,给了我以下输出。

var result=$x("//div[@id='index__tree']//a[contains(text(),doku.php)]/@href")

result[0].value
"/doku.php?ihome"
result[4].value
"/doku.php?start"

我将 xpath 转换为 selenium 代码:​​

a = driver.find_elements_by_xpath("//div[@id='index__tree']//a[contains(text(),doku.php)]/@href")

for aa in a:
        print(aa)

然后我运行代码并收到以下错误:

opening browser
Login Successful
Traceback (most recent call last):
  File "wiki.py", line 49, in <module>
    a = driver.find_elements_by_xpath("//div[@id='index__tree']//a[contains(text(),doku.php)]/@href")
  File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 410, in find_elements_by_xpath
    return self.find_elements(by=By.XPATH, value=xpath)
  File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 1007, in find_elements
    'value': value})['value'] or []
  File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/aevans/wikiProject/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[@id='index__tree']//a[contains(text(),doku.php)]/@href" is: [object Attr]. It should be an element.
  (Session info: headless chrome=73.0.3683.86)
  (Driver info: chromedriver=73.0.3683.86,platform=Linux 3.10.0-957.12.2.el7.x86_64 x86_64)

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver xpath


    【解决方案1】:

    尝试替换

    a = driver.find_elements_by_xpath("//div[@id='index__tree']//a[contains(text(),doku.php)]/@href")
    for aa in a:
        print(aa)
    

    a = [elem.get_attribute("href") for elem in driver.find_elements_by_xpath("//div[@id='index__tree']//a[contains(text(),doku.php)]")]
    
    for aa in a:
        print(aa)
    

    请注意,我从您的选择器末尾删除了“/@href”。

    Selenium 选择器必须返回一个 WebElement。通过指定“/@href”,它返回了该元素的 href 属性,而不是元素本身。

    get_attribute(attribute_name) 方法返回元素的属性。然后,你可以循环遍历它。

    【讨论】:

    • 您的意思是在 a=[ elem 之后添加“[”。我在使用此代码的 for 循环中遇到语法错误。我试图删除 [ 但我收到错误“for elem in ...”中的“for”
    • 我忘记在列表理解的末尾添加右括号“]”。已编辑
    • 原来的 xpath 给我带来了大约 157 个链接。这段代码让我大约 20 岁。有什么想法吗?
    • 您是否确保脚本正在等待所有链接加载完毕?
    • 我在脚本中添加了一个 time.sleep(5) 并且它不会产生更多链接
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 2021-01-29
    • 2023-03-30
    • 1970-01-01
    • 2013-02-24
    • 2016-03-17
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多