Home > Software design >  'str' object is not callable error while using 'Find an element' by link text or
'str' object is not callable error while using 'Find an element' by link text or

Time:04-26

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 -

  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").

  • Related