' str'使用'查找元素&#x27时,对象不是可呼叫错误。通过链接文本或XPATH中的Selenium Python

发布于 2025-01-24 05:31:39 字数 481 浏览 0 评论 0原文

我是编码的新手,并尝试与Python一起学习硒。我希望单击下载按钮,但是正在收到错误“'str'对象不可呼应”。使用Python 3.10和Selenium 4.1.3

这是我的代码:

import os

from selenium import webdriver

from selenium.webdriver.common.by import By

os.environ['PATH'] += r"C:\SeleniumDrivers"

driver = webdriver.Chrome()

driver.get("https://www.python.org/downloads")

driver.implicitly_wait(10)

my_element = driver.find_element(By.XPATH("//a[contains(@href,'Download Python 3.10.4')]"))

my_element.click()

I'm new to coding and trying to learn Selenium with Python. I want the download button to be clicked, but am getting the error"'Str' object not callable".Using Python 3.10 and Selenium 4.1.3

Here is my code:

import os

from selenium import webdriver

from selenium.webdriver.common.by import By

os.environ['PATH'] += r"C:\SeleniumDrivers"

driver = webdriver.Chrome()

driver.get("https://www.python.org/downloads")

driver.implicitly_wait(10)

my_element = driver.find_element(By.XPATH("//a[contains(@href,'Download Python 3.10.4')]"))

my_element.click()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天赋异禀 2025-01-31 05:31:39

我认为您想单击“下载python”`。如果是的,则此代码可以完成工作,

my_element = driver.find_element(by = By.XPATH, value = "//*[@id='touchnav-wrapper']/header/div/div[2]/div/div[3]/p/a")
my_element.click()

您的代码有两个问题 -

  1. 您做了此by.xpath(“ // a [continains(@href,'下载python 3.10.4')]”)< /代码>。这里by.xpath实际上不是一个函数,而只是一个具有“ xpath”值的变量,因此您无法传递其中的参数。这就是为什么您获得'str'对象不是可呼叫错误的原因。对于xpath,您需要使用此类内容driver.find_element(by = by.xpath,value =&lt; xpath&gt;)


  2. 即使您在上面所做的事情是正确的,您所经过的XPATH是不正确的。正确的XPath是my_element = driver.find_element(by = by.xpath,value =“ //*[@ID ='touchnav-wrapper']/header/header/div/div/div/div/div/div/div [3 ]/p/a“)

I assume that you wanted to click on "Download Python"`. If yes then this code does the job,

my_element = driver.find_element(by = By.XPATH, value = "//*[@id='touchnav-wrapper']/header/div/div[2]/div/div[3]/p/a")
my_element.click()

Your code has two problems -

  1. You did this By.XPATH("//a[contains(@href,'Download Python 3.10.4')]"). Here By.XPATH is not actually a function but just a variable with the value "XPATH", so you can't pass parameters in it. That's why you got the 'str' object is not callable error. For XPATHs you need to use something like this driver.find_element(by = By.XPATH, value = <XPATH>).

  2. Even if what you did above was correct, the XPATH you were passing was incorrect. The correct XPATH is my_element = driver.find_element(by = By.XPATH, value = "//*[@id='touchnav-wrapper']/header/div/div[2]/div/div[3]/p/a").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文