简体   繁体   中英

Unable to click element after select by XPATH Selenium Python

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/")

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM