Home > Software engineering >  Selenium (browser.find_element(by=By.PARTIAL_LINK_TEXT) error
Selenium (browser.find_element(by=By.PARTIAL_LINK_TEXT) error

Time:11-20

I need to find a partial link button on the page and click on it. But it gives me an error.

Code:

button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()

Link

Error:

button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()
TypeError: 'str' object is not callable

CodePudding user response:

There are 2 problems here:

  1. Instead of
browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch'))

it should be

browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
  1. In case you want to click on that element on the same line you should not try to put the result to button object. Because browser.find_element(By.PARTIAL_LINK_TEXT('/watch')) returns an object of web element so you can click on it with .click() method. However .click() method itself returns void i.e. nothing. So your code can be
browser.find_element(By.PARTIAL_LINK_TEXT('/watch')).click()

or

button = browser.find_element(By.PARTIAL_LINK_TEXT('/watch'))
button.click()

CodePudding user response:

This is the wrong way

button = browser.find_element(by=By.PARTIAL_LINK_TEXT('/watch')).click()

You should use this :

button = browser.find_element(By.PARTIAL_LINK_TEXT, "/watch")
button.click()

Also, If You see the method signature is

def find_element(self, by=By.ID, value=None):

It is expected to pass By then separate that with the actual locator.

Also, I would recommend having WebDriverWait (ExplicitWait) for this use case.

button = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "/watch")))
button.click()

You will have to import these as well.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

You were close enough. The actual implementation is:

self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)

So you need to pass two parameters,

  • The by parameter`:

    By.PARTIAL_LINK_TEXT
    
  • The value parameter within quotes:

    "/watch"
    

So your effective line of code will be:

browser.find_element(By.PARTIAL_LINK_TEXT, "/watch")

Finally, click() doesn't returns anything, so if to try to assign it to a variable, it will be always NULL. So better to drop it.

So your working line of code will be:

browser.find_element(By.PARTIAL_LINK_TEXT, "/watch").clcik()

Ideally to invoke click() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy:

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "/watch"))).click()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC    
  • Related