Selenium cannot find an element

111 Views Asked by At

I am trying to get selenium to find an element and click on it.

The element is as follows:

<button class="btn btn-link">search criteria: on support work 'took place on' (grouped by author), from: 2020-10-01, to: 2020-12-31</button>

When clicked, this button opens a form within the same URL that allows me to input an upper and lower time restraint. The goal is to get Selenium to fill in the form and click 'Update'.

I've tried a few different methods ('find_element_by_xpath', '...by_class_name', 'WebDriverWait' etc but selenium doesn't ever recognise the button element. Ive all tried skipping this step and trying to fill in the form directly (since its all under the same URL but no luck.

I'm a total beginner so the solution is probably very basic. I just don't know what it is.

4

There are 4 best solutions below

3
On

Try: driver.findElement(By.cssSelector('btn.btn-link')).

0
On

Check if the element is inside a parent with tag iframe:

if it is then you have to first swtich to that iframe:

find the iframe webelement using xpath and then switch to it, and then do the button click

iframe = driver.find_element_by_xpath("//iframe[@id='something']")
driver.switch_to.frame(iframe)

//do your code
// then switch back to main frame
driver.switch_to.default_content()
0
On

To click on the element with text as search criteria: on support work 'took place on' (grouped by author), from: 2020-10-01, to: 2020-12-31 you can use either of the following Locator Strategies:

  • Using partial_link_text:

    driver.find_element_by_partial_link_text("search criteria: on support work").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//button[@class='btn btn-link' and starts-with(., 'search criteria: on support work')]").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "search criteria: on support work"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-link' and starts-with(., 'search criteria: on support work')]"))).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
    
2
On

If you cant locate it by any chance, and also not by an explicit wait, you might need to use pyautogui to select the position of the button, then move the mouse there and click. If you need to scroll down, to make it visible, make sure you implement a scrolling script