通过 execute_script 调用的 scrollBy() 在使用 Selenium 和 Python 的 chrome 商店页面上不起作用
scrollBy() invoked through execute_script is not working on chrome store page using Selenium and Python
我将 selenium 与 python 3 和 Chrome 驱动程序一起使用。我正在尝试在 chrome 商店 URL 中执行滚动。
这个:https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh
无论我怎样尝试,网站都无法滚动。尝试了 运行 "window.scrollBy" 和 "window.scrollTo"。
尝试先点击 "body",然后点击其他元素 - 没有。
当前代码:
driver.maximize_window()
driver.get(url)
time.sleep(4)
driver.execute_script("window.scrollBy(0, 500)")
可能是什么问题?我不想发送 "down" 密钥或 "page down",因为我认为我需要更精确。
将元素滚动到视图中似乎在该网页上运行良好。这是一个例子:
footer=driver.find_element_by_class_name('e-f-ra-gj')
driver.execute_script("arguments[0].scrollIntoView()", footer)
如果您观察到 chrome store page 页面有一个固定在页面顶部的页眉。所以当 window 对象被调用时,焦点跳到页面的顶部,将 document 留在固定页眉后面。
You can find a detailed discussion in offsetting an html anchor to adjust for fixed header
此外,有时 Chrome驱动程序非常快并且 scrollTo()
操作在 Chrome 默认滚动到 html 锚点事件之前触发。在这些情况下,一个可能的解决方案是引入一些延迟,如下所示:
setTimeout(function() {window.scrollTo(0, y);},1)
You can find a detailed discussion in JavaScript issue with scrollTo() in Chrome
作为替代方案,执行基于 within the chrome store URL you need to induce WebDriverWait for the visibility_of_element_located()
of your choice and you can use the following 的解决方案:
使用scrollIntoView()
:
driver.get('https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh')
driver.execute_script("arguments[0].scrollIntoView();", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='Related']"))))
浏览器快照:
我将 selenium 与 python 3 和 Chrome 驱动程序一起使用。我正在尝试在 chrome 商店 URL 中执行滚动。
这个:https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh
无论我怎样尝试,网站都无法滚动。尝试了 运行 "window.scrollBy" 和 "window.scrollTo"。 尝试先点击 "body",然后点击其他元素 - 没有。
当前代码:
driver.maximize_window()
driver.get(url)
time.sleep(4)
driver.execute_script("window.scrollBy(0, 500)")
可能是什么问题?我不想发送 "down" 密钥或 "page down",因为我认为我需要更精确。
将元素滚动到视图中似乎在该网页上运行良好。这是一个例子:
footer=driver.find_element_by_class_name('e-f-ra-gj')
driver.execute_script("arguments[0].scrollIntoView()", footer)
如果您观察到 chrome store page 页面有一个固定在页面顶部的页眉。所以当 window 对象被调用时,焦点跳到页面的顶部,将 document 留在固定页眉后面。
You can find a detailed discussion in offsetting an html anchor to adjust for fixed header
此外,有时 Chrome驱动程序非常快并且 scrollTo()
操作在 Chrome 默认滚动到 html 锚点事件之前触发。在这些情况下,一个可能的解决方案是引入一些延迟,如下所示:
setTimeout(function() {window.scrollTo(0, y);},1)
You can find a detailed discussion in JavaScript issue with scrollTo() in Chrome
作为替代方案,执行基于 visibility_of_element_located()
of your choice and you can use the following
使用
scrollIntoView()
:driver.get('https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh') driver.execute_script("arguments[0].scrollIntoView();", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='Related']"))))
浏览器快照: