' str'使用'查找元素&#x27时,对象不是可呼叫错误。通过链接文本或XPATH中的Selenium Python
我是编码的新手,并尝试与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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想单击“下载python”`。如果是的,则此代码可以完成工作,
您的代码有两个问题 -
您做了此
by.xpath(“ // a [continains(@href,'下载python 3.10.4')]”)< /代码>。这里
by.xpath
实际上不是一个函数,而只是一个具有“ xpath”值的变量,因此您无法传递其中的参数。这就是为什么您获得'str'对象不是可呼叫错误的原因。对于xpath,您需要使用此类内容driver.find_element(by = by.xpath,value =&lt; xpath&gt;)
。即使您在上面所做的事情是正确的,您所经过的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,
Your code has two problems -
You did this
By.XPATH("//a[contains(@href,'Download Python 3.10.4')]")
. HereBy.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 thisdriver.find_element(by = By.XPATH, value = <XPATH>)
.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")
.