Home > Mobile >  Unable to click element after select by XPATH Selenium Python
Unable to click element after select by XPATH Selenium Python

Time:08-07

I am trying to click the "Sign In" button by XPATH and still have issues with this on on certain websites (indeed.com, twitter.com), but it works just fine on others (ziprecruiter.com). Initially, I was trying by class but hit a block with multi-class.

It print the object just fine, but gives an error on click().

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Safari()
def startBot(userName,passwordj,url):
    driver.get('https://www.indeed.com/')
    driver.maximize_window()
    
    a = driver.find_element(By.XPATH, '//*[@id="gnav-main-container"]/div/div/div[2]/div[2]/div[2]/a')
   
    print(a)
    
    a.click()

startBot("","","https://indeed.com/")

CodePudding user response:

Sometimes the usual click doesn't work, then you need to use javascript to click on the button, here is an example of the code:

element=driver.find_element(By.XPATH,'//*[@id="gnav-main-container"]/div/div/div[2]/div[2]/div[2]/a')
driver.execute_script("arguments[0].click();", element)

CodePudding user response:

To click on the element with text as Sign in you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using XPATH:

    driver.execute("get", {'url': 'https://in.indeed.com/?r=us'})
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'https://secure.indeed.com/account/login') and text()='Sign in']"))).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