我有以下代码 -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
Create = driver.find_element_by_xpath("//*[@title='Create Incident']")
Create.click()
每当我在脚本或批处理中运行此代码时,我都会遇到错误 -
Traceback (most recent call last):
File "P:\Selenium\helpline1.py", line 15, in <module>
Create.click()
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a id="anc" class="RedirectTab" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("anc", "", true, "", "", false, true))"> is not clickable at point (207.1666717529297,25) because another element <div id="overley" class="over_ley"> obscures it
登录该站点发生得非常完美,但在点击元素后出现此错误(即下一行代码)。
此外,当我在Python IDLE或Python CLI中手动逐行运行此代码时,它运行时没有任何错误。在IDLE或Python CLI中以脚本/批处理模式发布
感谢建议。
答案 0 :(得分:5)
如果您在尝试执行点击时显示了叠加层,则可能需要等待叠加层关闭:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
Create = driver.find_element_by_xpath("//*[@title='Create Incident']")
WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
Create.click()
答案 1 :(得分:2)
试试这个:
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
element = wait.until(EC.element_to_be_clickable((By.XPath, "//*[@title='Create Incident']")))
Create = driver.find_element_by_xpath("//*[@title='Create Incident']")
Create.click()
通过等待,您将有时间浏览器加载de按钮,然后尝试执行单击,这将在元素准备时发生。