我试图通过Selenium抓住这个site。
我想点击"下一页" buttom,为此我做了:
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>
始终为this page
我试过这个
driver.implicitly_wait(10)
el = driver.find_element_by_class_name('pagination-r')
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 918, 13)
action.click()
action.perform()
但是我得到了同样的错误
答案 0 :(得分:66)
另一个元素是覆盖要单击的元素。您可以使用execute_script()
点击此内容。
element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].click();", element)
答案 1 :(得分:13)
我有一个类似的问题,使用ActionChains没有解决我的错误: WebDriverException:消息:未知错误:元素在点(5处)不可单击 74,892)
如果您不想使用execute_script,我找到了一个很好的解决方案:
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
或
inputElement.send_keys(Keys.SPACE) #for checkbox etc
答案 2 :(得分:5)
由于元素在浏览器中不可见,首先需要向下滚动到元素 这可以通过执行 javascript 来执行。
element = driver.find_element_by_class_name('pagination-r')
driver.execute_script("arguments[0].scrollIntoView();", element)
driver.execute_script("arguments[0].click();", element)
答案 3 :(得分:2)
我已经编写了处理此类异常的逻辑。
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
答案 4 :(得分:1)
使用显式等待而不是隐式。
new WebDriverWait(TestingSession.Browser.WebDriver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.ClassName("pagination-r'"))));
答案 5 :(得分:0)
即使在元素上使用wait之后,如果仍然收到element not clickable
错误,请尝试以下解决方法之一:
Action
移至element
的位置,然后在perform
上运行action
WebElement element = driver.findElement(By("element_path"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().perform();`
element
和wait
上的覆盖层或微调器是否不可见By spinnerimg = By.id("spinner ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(spinnerimg ));
希望这会有所帮助