在Selenium Python中向下滚动Keys.PAGE_
大家好,有人能帮我滚动吗 我想使用在Selenium Python中向下滚动Keys.PAGE_,python,selenium-webdriver,web-scraping,lazy-loading,webdriverwait,Python,Selenium Webdriver,Web Scraping,Lazy Loading,Webdriverwait,大家好,有人能帮我滚动吗 我想使用 actions = ActionChains(browser) actions.send_keys(Keys.PAGE_DOWN) actions.perform() 直到它到达滚动的底部,在那里它会找到一个元素“加载更多” 然后ponce单击load more按钮,它必须再次执行滚动操作,然后再次执行loadmore操作,直到load more按钮不可用 我必须使用这个page down操作,因为元素在页面滚动之前是不会加载的,如果有人能建议一些解决方案,
actions = ActionChains(browser)
actions.send_keys(Keys.PAGE_DOWN)
actions.perform()
直到它到达滚动的底部,在那里它会找到一个元素“加载更多”
然后ponce单击load more按钮,它必须再次执行滚动操作,然后再次执行loadmore操作,直到load more按钮不可用
我必须使用这个page down操作,因为元素在页面滚动之前是不会加载的,如果有人能建议一些解决方案,那么这个元素将非常有帮助
@PedroLobito我正在检索产品链接,你能帮我吗 在这个
无需
selenium
在这种情况下,只需通过developer tools
嗅探xhr
请求,直接进入黄金(json
)
产品的
url
结构如下:
https://www.x.com/product/anything-Item#
只需在url末尾的中添加Item#
值,如下所示:
https://www.x.com/product/anything-5P540
https://www.x.com/product/anything-5P541
py3
示例(对于py2
,只需更改格式
语法):
滚动页面
https://www.grainger.com/category/black-pipe-fittings/pipe-fittings/pipe-tubing-and-fittings/plumbing/ecatalog/N-qu1?searchRedirect=products
直到它到达页面底部,在那里它将找到一个文本为“查看更多”的元素,然后单击该元素,直到该元素不可用您可以使用以下解决方案:
- 代码块:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import StaleElementReferenceException from selenium.common.exceptions import TimeoutException options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') browser.get("https://www.grainger.com/category/black-pipe-fittings/pipe-fittings/pipe-tubing-and-fittings/plumbing/ecatalog/N-qu1?searchRedirect=products") while True: try: browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='btn list-view__load-more list-view__load-more--js' and normalize-space()='View More']")))) browser.execute_script("arguments[0].click();", WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn list-view__load-more list-view__load-more--js' and normalize-space()='View More']")))) print("View More button clicked") except (TimeoutException, StaleElementReferenceException) as e: print("No more View More buttons") break browser.quit()
- 控制台输出:
View More button clicked View More button clicked No more View More buttons
from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.PAGE_DOWN)
您试图从该网页检索什么?是这个吗?如果是这样,你就不需要硒了,只需向上面的链接发出请求,并将
breadcrumbCatId
更改为所需的类别
@PedroLobito我正在尝试取消产品链接,你能帮我吗?链接显示了2144种产品,但我没有得到所有这些解决方案。解决方案只是一个起点。您必须创建一个循环来检索其余的产品。当我在a上时,我会看一看它。你能建议我一个类似的解决方案吗?因为它包含23159个产品,我想提取元素li中属性“data-url-ie8”中提到的产品的所有链接
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import TimeoutException
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
browser.get("https://www.grainger.com/category/black-pipe-fittings/pipe-fittings/pipe-tubing-and-fittings/plumbing/ecatalog/N-qu1?searchRedirect=products")
while True:
try:
browser.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(browser,10).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='btn list-view__load-more list-view__load-more--js' and normalize-space()='View More']"))))
browser.execute_script("arguments[0].click();", WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn list-view__load-more list-view__load-more--js' and normalize-space()='View More']"))))
print("View More button clicked")
except (TimeoutException, StaleElementReferenceException) as e:
print("No more View More buttons")
break
browser.quit()
View More button clicked
View More button clicked
No more View More buttons
from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.PAGE_DOWN)