selenium执行click报错的解决方案

在执行

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()
发布了65 篇原创文章 · 获赞 52 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/zhangenter/article/details/104113042