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()
CodePudding user response:
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 -
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")
.