简体   繁体   English

无法在 select by XPATH Selenium ZA7F5F35426B9278211FCZ23135B 之后单击元素

[英]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).我正在尝试单击 XPATH 的“登录”按钮,但在某些网站上仍然存在问题(确实。com,ZB73C2D22763D1CE2143A3755C1.D0AD3AZ.com) Initially, I was trying by class but hit a block with multi-class.最初,我尝试使用 class 但遇到了多类问题。

It print the object just fine, but gives an error on click().它可以很好地打印 object,但在 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:有时通常的点击不起作用,这时需要使用javascript来点击按钮,下面是代码示例:

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 :要单击带有文本作为登录的元素,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下定位器策略

  • Using XPATH :使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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