-1

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>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 3
    You are not using the `find_element` function correctly: `driver.find_element(By.XPATH, '//a[contains(("This is my text (Make a selection)"')` – dosas Jul 05 '22 at 20:21

2 Answers2

-1

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']]")
    

Update

To print the element text:

  • xpath using the text This is my text:

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

    print(driver.find_element(By.XPATH, "//li[contains(., 'Make a selection')][.//input[starts-with(@name, 'intent') and @type='radio']]").text)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you so much! This worked. Wish I would have asked this earlier. – Eric Smith Jul 06 '22 at 12:10
  • undetected Selenium, how can I use the whole text to find the element - I have tried escaping the parenthesis with no luck. 'This is my text /(Make a selection/)'. Also, is it possible to print the text from this element? For example, with //li[contains(., 'Make a selection') I would like to print `This is my text (Make a selection)` – Eric Smith Jul 06 '22 at 14:01
  • @EricSmith there are two entirely different questions, 1) how can I use the whole text to find the element & 2) is it possible to print the text from this element. Feel free to raise a new question as per your new requirement please. – undetected Selenium Jul 06 '22 at 14:35
  • I have requirements for both but if I could get 2)the text from the element that may help with requirement 1. Thanks – Eric Smith Jul 06 '22 at 15:39
  • @EricSmith Checkout the updated answer and let me know the status. – undetected Selenium Jul 06 '22 at 17:21
  • This is giving me InvalidSelectorException. – Eric Smith Jul 06 '22 at 18:38
  • There was a typo which I corrected. Retest and let me know the result. – undetected Selenium Jul 06 '22 at 18:58
  • What was the typo? I am getting the same error and they look the same. This is my first post to stackoverflow so it might be user error. – Eric Smith Jul 06 '22 at 19:43
  • my bad, I updated the first two locators but didn't update the last two. Please check now. – undetected Selenium Jul 06 '22 at 19:47
-1

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')]"))
Sonali Das
  • 943
  • 1
  • 7
  • 24