zl程序教程

您现在的位置是:首页 >  Python

当前栏目

selenium执行click报错的解决方案

2023-03-14 22:55:26 时间

在执行

driver.find_element_by_class_name('xxx').click()
复制代码

操作时可能出现如下提示错误: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="xxx">...</div> is not clickable at point (500, 600). Other element would receive the click: <div class="yyy">...</div>

错误原因,元素被遮挡,可以先使用下面的方法将元素滚动到可见区域

driver.execute_script('arguments[0].scrollIntoView(true)',driver.find_element_by_class_name('xxx'));
复制代码

这个滚动以后元素会滚到视图顶部,但是有的页面顶部也有遮挡,滚到顶部以后可能会被其他元素遮挡,继续报上面的错误。这时候可以在上面代码的基础上再加一个y轴回滚100像素的处理。

driver.execute_script('scrollBy(0,-100)')
复制代码

现在再执行click就没这问题了,完整的代码

driver.execute_script('arguments[0].scrollIntoView(true)',driver.find_element_by_class_name('xxx'));
driver.execute_script('scrollBy(0,-100)')
driver.find_element_by_class_name('xxx').click()