Python Selenium类实例中滚动失效引发元素点击拦截问题
Selenium滚动元素后点击失败:控制台可行,类方法内无效
问题背景
我正在编写Python Selenium脚本从网站下载账单,当账单行数超出视口范围后,脚本执行失败。
已尝试的滚动方案
ActionChains(self.driver).move_to_element(element).perform()→ 无效element.location_once_scrolled_into_view→ 在PyCharm控制台执行有效self.driver.execute_script("window.scrollTo({});")→ 在PyCharm控制台执行有效
异常情况
方案2和3在PyCharm控制台逐行执行时表现正常:侧边栏会滚动到目标元素位置,元素也能正常点击。但将代码放入类实例的downloader方法中运行时,滚动条完全不移动,同时抛出以下错误:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (1187, 642)
相关代码片段
def downloader(self, count, provider): print("count: ", count) # count是对应服务商的账单行数 for number in range(1, (count + 1)) : #sleep(2) element = self.wait.until(EC.visibility_of( self.driver.find_element(By.XPATH, "//table/tbody/tr[{}]/td[6]".format(number)))) location = element.location_once_scolled_into_view # ActionChains(self.driver).move_to_element(element).perform() #self.driver.execute_script("arguments[0].scrollIntoView(true);",element); # self.wait.until(EC.element_to_be_clickable(element)) # sleep(1) self.driver.execute_script("window.scrollTo({});".format(location)) element.click()
内容的提问来源于stack exchange,提问作者Zsolt
相关产品推荐
相关产品推荐

