Python 滚动到某个元素并将鼠标悬停在其上

Python 滚动到某个元素并将鼠标悬停在其上,python,selenium,web-scraping,Python,Selenium,Web Scraping,我想做一个循环,将鼠标悬停在元素上,但当我按类名查找元素时,它也会给出位于视口边界之外或之上的元素。我在下面尝试了这段代码,其中我执行了任意数量的滚动,并且在每一个滚动中,我只尝试将鼠标悬停在视口的y=50到y=900范围内的元素上: numofscrolls=20 for iscrolls in range(1,numofscrolls): ymin=0 targets=currentpage.find_elements_by_class_name('css-4rbku5') for i in

我想做一个循环,将鼠标悬停在元素上,但当我按类名查找元素时,它也会给出位于视口边界之外或之上的元素。我在下面尝试了这段代码,其中我执行了任意数量的滚动,并且在每一个滚动中,我只尝试将鼠标悬停在视口的y=50y=900范围内的元素上:

numofscrolls=20
for iscrolls in range(1,numofscrolls):
ymin=0
targets=currentpage.find_elements_by_class_name('css-4rbku5')
for i in range(0,len(targets)):          
    if targets[i].location['y']<=900+ymin and targets[i].location['y']>=ymin+50:
       hoverchain=action_chains.ActionChains(browser)
       hovernow=hoverchain.move_to_element(targets[i])
       hovernow.perform()
       browser.execute_script("window.scrollTo(0, 1500)")
       ymin=ymin+1500

请修正你的格式。在Python中,任何错误的选项卡都可能产生不同类型的结果。很抱歉,这是我第一次在手机上使用堆栈溢出。我会尽快修好的。无论如何,if循环在for循环内。当我使用它时,我没有缩进错误。实际上,你的代码应该会产生错误,因为循环的第一个
没有半列(
),
numofrolls
一次使用大写
N
,另一次使用小写
N
<代码>用于循环
带大写字母
F
。。。你在用microsoft word写代码吗?谢谢,不用了。我在这里发布的时候只是想简化一下。我删除了不相关的行。所以这些只是我发帖时的打字错误。我现在改正了
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)

MoveTargetOutOfBoundsException: (206, 1476) is out of bounds of viewport width (939) and height (973)