我的Python Selenium
无法单击目标元素,而是单击元素位于目标元素后面?
我尝试单击,或在“下拉菜单”中输入文本,但是我发现单击此“下拉菜单”后面的元素会导致结果。我知道这是其背后的要素,因为后面有咨询区域,并且结果自上而下显示了相同的咨询材料。这是我的代码:
# info for login
my_email = 'my_email'
my_passcode = 'my_passcode'
email_url = r'https://www.gmx.com/#.1559516-header-navlogin2-1'
# start driver and open url
driver = webdriver.Chrome(chrome_path)
driver.get(email_url)
# input email account
xpath = r'//*[@id="login-email"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_email).perform()
# input passcode and hit 'enter' to login
xpath = r'//input[@id="login-password"]'
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target = driver.find_element_by_xpath(xpath)
actions = ActionChains(driver)
actions.move_to_element(target).perform()
actions.click().send_keys(my_passcode).send_keys(Keys.ENTER).perform()
对于其他一些站点,当站点似乎具有“两层”时(对我是否使用了正确的词),这对我来说是偶然的。我可以在顶层处理任何东西,只能激活它后面的任何东西。非常感谢您提供解决方案!
答案 0 :(得分:1)
我在脚本中的任何位置都看不到登录按钮的clickign,您可以使用XPath contains()
function来找到相关的按钮,例如:
xpath = r'//button[contains(@class,"login-submit")]'
然后只需对结果变量调用click()
函数
建议的示例代码:
我在脚本中的任何位置都看不到登录按钮的clickign,您可以使用XPath contains()
function来找到相关的按钮,例如:
xpath = r'//button[contains(@class,"login-submit")]'
然后只需对结果变量调用click()
函数
建议的示例代码:
# input email account
xpath = r'//*[@id="login-email"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_email)
# input passcode and hit 'enter' to login
xpath = r'//input[@id="login-password"]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.send_keys(my_passcode)
xpath = r'//button[contains(@class,"login-submit")]'
target = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
target.click()