Home > Enterprise >  Selenium Python TypeError: 'str' object is not callable using Python version 3.8 and Selen
Selenium Python TypeError: 'str' object is not callable using Python version 3.8 and Selen

Time:07-06

I have Googled this many times and am unable to figure this out. I have the following code and am trying to pull the element based on either the specific string or a piece of the string. The id and data-id can change so those are not usable. I have tried lots of variations of the same thing but continue to get the str object is not callable.

driver.find_element(By.XPATH, '//a[contains(("This is my text (Make a selection)")]')
driver.find_element(By.XPATH, "//a[contains(text(), 'Make a selection')]")

HTML:

<li id="li_L2_SR_6">
    <input name="intent1" type=
    "radio" data-id="L2_SR_6">
    "This is my text (Make a selection)"
</li>

CodePudding user response:

This error message...

TypeError: 'str' object is not callable

...indicates you have passed incorrect argument type.

find_element() takes two arguments, the By implementation and the locator strategy.


Solution

As per the HTML:

<li id="li_L2_SR_6">
    <input name="intent1" type="radio" data-id="L2_SR_6">
    "This is my text (Make a selection)"
...
...
</li>

To identify the element you can use either of the following Locator Strategies:

  • xpath using the text This is my text:

    element = driver.find_element(By.XPATH, "//li[contains(., 'This is my text')]//input[starts-with(@name, 'intent') and @type='radio']")
    
  • xpath using the text Make a selection:

    element = driver.find_element(By.XPATH, "//li[contains(., 'Make a selection')]//input[starts-with(@name, 'intent') and @type='radio']")
    

CodePudding user response:

There is no link but you are using a tag. Please try with

driver.find_element(By.XPATH("//li[text()='This is my text \(Make a selection\)']"))

Even this will also work

driver.find_element(By.XPATH("//li[contains(.,'This is my text')]"))
  • Related