如何解决硒通过扭曲的Python滚动进入视图
我正在尝试将元素滚动到视图中以便能够对它们进行处理..通常使用滚动到视图中..或动作链来完成,但是滚动不在窗口上..它在窗口中。因为在窗口中有一个单独的滚动条。该页面确实具有普通的窗口滚动条,但是使用该滚动条不会将所需的元素显示在视图中。 所以我的问题是..是否可以向下滚动内部滚动条?
蓝色箭头是试图进行滚动的滚动
我知道这样一个事实,即随着iv手动滚动以使元素进入视图,滚动此代码可以防止代码崩溃,但是我需要它自动执行。 请注意。.屏幕略微缩小以显示更多元素..我认为这可以工作,但是如果我在“活跃交易”栏中有更多元素,则该技术将过时。
我要移动的滚动条的CSS选择器在这里
driver1.executeScript("arguments[0].scrollIntoView(true);",driver1.find_element_by_css_selector("#mCSB_2_dragger_vertical > .mCSB_dragger_bar"));
the
编辑: 聚集一些会出错的代码
scrollbar = str(driver1.find_element_by_xpath("//div[@id='mCSB_2_dragger_vertical']").get_attribute("style"))
print(scrollbar)
new_style = scrollbar.get_attribute('style').replace('top: 0px','top 200px')
driver1.execute_script('arguments[6].setAttribute("style","%s")' % new_style,scrollbar)
print(scrollbar)
引发此错误
new_style = scrollbar.get_attribute('style').replace('top: 0px','top 200px')
AttributeError: 'str' object has no attribute 'get_attribute'
发现此链接有助于..ish
How can I change an attribute value in the DOM using Selenium and Python
edit2:
尝试了多种方法来实现它。.我认为我在这里可以使用的一种方法只是需要调整
#scrollbar = str(driver1.find_element_by_xpath("//div[@id='mCSB_2_dragger_vertical']").get_attribute("style"))
#print(scrollbar)
#new_style = driver1.find_element_by_xpath("//div[@id='mCSB_2_dragger_vertical']").get_attribute("style").replace('top: 0px','top: 200px')
#driver1.execute_script('arguments[6].setAttribute("style",scrollbar)
#print(scrollbar)
element = driver1.find_element_by_xpath("//div[@id='mCSB_2_dragger_vertical']")
#A = driver1.execute_script("argument[0]",element)
driver1.execute_script("arguments[0].setAttribute('style','top: 300px')",element)
#A =driver1.execute_script("arguments[0].value = 'top: 100px'",element)
#print(A)
#driver1.execute_script("arguments[0].setAttribute('top:',element)
#div_elem = driver1.find_element_by_xpath("//div[@id='mCSB_2_dragger_vertical']")
#print(div_elem.text)
#new_style = div_elem.get_attribute('style').replace('top: 0px','top: 200px')
#driver1.execute_script('arguments[0].setAttribute("style",div_elem)
#print(div_elem.text)
#newvalue = "200px"
#splitting = scrollbar.split(": ")
#indextochange = str(splitting[6])
#EntirenewAttribute = str(scrollbar.replace(indextochange,newvalue))
#driver1.execute(scrollbar.replace(scrollbar,EntirenewAttribute))#EntirenewAttribute
#driver1.execute_script("arguments[0].value = arguments[1]",scrollbar,top = "200")
# print(scrollbar)
解决方法
通常,即使您具有内部滚动条,硒的常规click()操作也应为您处理滚动。如果这样不起作用,则需要使用JS scrollIntoView
在这种情况下,您尝试使用JS是正确的,但是您的选择器似乎过于具体,与您想要的元素不匹配。
尝试在浏览器中按F12键(将打开调试工具)。
然后在控制台中键入该JS代码:
document.querySelector("#mCSB_2_dragger_vertical > .mCSB_dragger_bar")
这样,您将看到选择器实际匹配的元素。然后更改它,使其与您在视图中真正想要的元素相匹配。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。