Python Selenium调试:元素在点(X,Y)处不可单击
我试着用硒来刮这个 我想点击“下一页”按钮,为此我做:Python Selenium调试:元素在点(X,Y)处不可单击,python,selenium,selenium-webdriver,web-scraping,selenium-firefoxdriver,Python,Selenium,Selenium Webdriver,Web Scraping,Selenium Firefoxdriver,我试着用硒来刮这个 我想点击“下一页”按钮,为此我做: driver.find_element_by_class_name('pagination-r').click() 它适用于许多页面,但不是所有页面,我得到了这个错误 WebDriverException: Message: Element is not clickable at point (918, 13). Other element would receive the click: <div class="linkAuch
driver.find_element_by_class_name('pagination-r').click()
它适用于许多页面,但不是所有页面,我得到了这个错误
WebDriverException: Message: Element is not clickable at point (918, 13). Other element would receive the click: <div class="linkAuchan"></div>
但是我得到了相同的错误另一个元素正在覆盖您尝试单击的元素。您可以使用
execute\u script()
单击此按钮
element=驱动程序。通过类名称(“分页-r”)查找元素
驱动程序。执行_脚本(“参数[0]。单击();”,元素)
使用显式等待而不是隐式等待
new WebDriverWait(TestingSession.Browser.WebDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.ClassName("pagination-r'"))));
我有一个类似的问题,使用ActionChains并不能解决我的错误: WebDriverException:消息:未知错误:元素在点(5)处不可单击 (74892) 如果您不想使用execute_脚本,我找到了一个很好的解决方案:
from selenium.webdriver.common.keys import Keys #need to send keystrokes
inputElement = self.driver.find_element_by_name('checkout')
inputElement.send_keys("\n") #send enter for links, buttons
或
如果您收到一个
元素不可点击
错误,即使在元素上使用了wait之后,也可以尝试以下解决方法之一:
- 使用
移动到操作
,然后在元素的位置
操作
- 检查
上的覆盖或微调器,以及元素
的不可见性等待
希望这有帮助我已经编写了处理此类异常的逻辑
def find_element_click(self, by, expression, search_window=None, timeout=32, ignore_exception=None,
poll_frequency=4):
"""It find the element and click then handle all type of exception during click
:param poll_frequency:
:param by:
:param expression:
:param timeout:
:param ignore_exception:list It is a list of exception which is need to ignore.
:return:
"""
if ignore_exception is None:
ignore_exception = []
ignore_exception.append(NoSuchElementException)
if search_window is None:
search_window = self.driver
end_time = time.time() + timeout
while True:
try:
web_element = search_window.find_element(by=by, value=expression)
web_element.click()
return True
except tuple(ignore_exception) as e:
self.logger.debug(str(e))
if time.time() > end_time:
self.logger.exception(e)
time.sleep(poll_frequency)
break
except Exception as e:
raise
return False
因为元素在浏览器上不可见,所以首先需要向下滚动到该元素 这可以通过执行javascript来执行
element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)
你能用python重写它吗。我从未在python上工作过,尽管您可以在python中获得显式等待的帮助。在这种情况下,ExpectedConditions.ElementExists将没有帮助。元素已找到,但不可单击当我转到该页面时,没有具有类名、
pagination-r
或linkAuchan
的元素。我猜页面已经更改了?@RemcoW这里的参数[0]
是什么意思?@chandresh执行脚本()
方法有两个参数。第一个是脚本,第二个是vararg,您可以在其中放置脚本中使用的任何参数。在本例中,我们只需要元素作为参数,但由于它是vararg,因此我们的元素是集合中的第一个元素。例如,您也可以执行driver.execute_脚本(“参数[0]。单击();参数[1]。单击();“element1,element2)
这将单击传递的两个元素请记住,如果您编写的测试打算像真实用户一样使用网站,那么您可能正在做一些真实用户无法做的事情,因为他们想要单击的元素已被覆盖。不要这样做只是为了让你的测试通过@CKM驱动程序。执行_脚本(“参数[0]。单击();”,元素)arguments[0]iselement
。您可以执行驱动程序。执行_脚本(“参数[0]。单击();doSmthElse(参数[1])”,元素,doSmthElseParam),在本例中为参数[1]
将是doSmthElseParam
我们可以在发送键后也单击吗???@AbhishekGupta-我们可以使用按键来模拟链接单击或按钮单击等动作,而不是使用鼠标。您需要两者的场景是什么?在我的情况下,其他一切都不起作用(复选框)。发送Keys.SPACE就像变魔术一样。您可以使用markdown来格式化答案中的代码,从而提高可读性。例如:WebElement element=driver.findElement(按(“元素路径”)代码>只需用反勾号字符包装代码:`它比其他可用选项更有效。Us Element单击忽略异常列表中的InterceptedException。非常好的解决方案!我在ignore_异常中添加了ElementClickInterceptedException和ElementNotInteractivatableException,将超时设置为3秒,工作起来很有魅力。参数[0]。scrollIntoView()代码>在当前接受的答案中缺失。这很好用。
WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();`
By spinnerimg = By.id("spinner ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(spinnerimg ));
def find_element_click(self, by, expression, search_window=None, timeout=32, ignore_exception=None,
poll_frequency=4):
"""It find the element and click then handle all type of exception during click
:param poll_frequency:
:param by:
:param expression:
:param timeout:
:param ignore_exception:list It is a list of exception which is need to ignore.
:return:
"""
if ignore_exception is None:
ignore_exception = []
ignore_exception.append(NoSuchElementException)
if search_window is None:
search_window = self.driver
end_time = time.time() + timeout
while True:
try:
web_element = search_window.find_element(by=by, value=expression)
web_element.click()
return True
except tuple(ignore_exception) as e:
self.logger.debug(str(e))
if time.time() > end_time:
self.logger.exception(e)
time.sleep(poll_frequency)
break
except Exception as e:
raise
return False
element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)