英文:
ScrollDown in table on a website usinig Selenium
问题
我尝试使用以下代码在网站上的表格中向下滚动:
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
WAIT = 1
print("Checking Browser driver...")
os.environ['WDM_LOG'] = '0'
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=srv, options=options)
waitWD = WebDriverWait(driver, 10)
link = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html"
driver.get(link)
driver.execute_script("arguments[0].scrollIntoView(true);", waitWD.until(EC.presence_of_element_located((By.XPATH, '//h2[text()="Cross Reference and Model Information"]'))))
tmpBODY = driver.find_element(By.XPATH, '//div[@class="m-bsc"]/a[@name="crossref"]')
for _ in range(5):
tmpBODY.send_keys(Keys.PAGE_DOWN)
但我始终收到以下错误消息:
(selenium) C:\DEV\Fiverr\TRY\biglaundrystore>python try.py
Checking Browser driver...
Traceback (most recent call last):
File "C:\DEV\Fiverr\TRY\biglaundrystore\try.py", line 31, in <module>
tmpBODY.send_keys(Keys.PAGE_DOWN)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
self._execute(
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=113.0.5672.127)
这是我想要向下滚动的表格 - 如您所见,页面向下滚动并不像预期那样工作:
英文:
i try to scroll down in a table on a website with the following code:
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
WAIT = 1
print(f"Checking Browser driver...")
os.environ['WDM_LOG'] = '0'
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
link = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html"
driver.get (link)
driver.execute_script("arguments[0].scrollIntoView(true);", waitWD.until(EC.presence_of_element_located((By.XPATH,'//h2[text()="Cross Reference and Model Information"]'))))
tmpBODY = driver.find_element(By.XPATH, '//div[@class="m-bsc"]/a[@name="crossref"]')
for _ in range(5):
tmpBODY.send_keys (Keys.PAGE_DOWN)
But i allways get this error-message:
(selenium) C:\DEV\Fiverr\TRY\biglaundrystore>python try.py
Checking Browser driver...
Traceback (most recent call last):
File "C:\DEV\Fiverr\TRY\biglaundrystore\try.py", line 31, in <module>
tmpBODY.send_keys (Keys.PAGE_DOWN)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
self._execute(
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webelement.py", line 404, in _execute
return self._parent.execute(command, params)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute
self.error_handler.check_response(response)
File "C:\DEV\.venv\selenium\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=113.0.5672.127)
This is the table i would like to scrolldown - as you can see the page-downs are not working as expected:
答案1
得分: 0
也许这是一个 iframe,你可以在页面源代码中搜索 "iframe",你应该首先进入 iframe,然后向下滚动。
你可以使用滚动来向下滚动。
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium.webdriver import ActionChains
from selenium import webdriver
scroll_origin = ScrollOrigin.from_viewport(0, 0)
ActionChains(driver).scroll_from_origin(scroll_origin, 0, kkk_y).perform()
这个答案可能会有帮助。
https://stackoverflow.com/a/76598852/17601704
查看文档:
https://www.selenium.dev/documentation/webdriver/actions_api/wheel/#scroll-to-element
https://www.selenium.dev/documentation/webdriver/interactions/frames/
英文:
Maybe it is a iframe and you can search "iframe" in page source, you should go to iframe firstly, then scroll down<br>
and you can use scroll to go down<br>
from selenium.webdriver.common.actions.wheel_input import ScrollOrigin
from selenium.webdriver import ActionChains
from selenium import webdriver
scroll_origin = ScrollOrigin.from_viewport(0, 0)
ActionChains(driver).scroll_from_origin(scroll_origin, 0, kkk_y).perform()
this answer maybe helpful
https://stackoverflow.com/a/76598852/17601704
view the document
https://www.selenium.dev/documentation/webdriver/actions_api/wheel/#scroll-to-element
https://www.selenium.dev/documentation/webdriver/interactions/frames/
答案2
得分: 0
我认为我找到了使用这段代码的解决方案:
(可能不是最优雅的解决方案,但似乎在最后做我需要的事情)
import os
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
WAIT = 1
print("Checking Browser driver...")
os.environ['WDM_LOG'] = '0'
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
link = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html"
driver.get (link)
lastCounts = 0
while True:
print(f"Reading elements found {lastCounts}")
driver.execute_script("arguments[0].scrollIntoView(true);", waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="xmodellist"]//tr[last()]'))))
time.sleep(WAIT)
soup = BeautifulSoup (driver.page_source, 'lxml')
worker = soup.find("div", {"id": "xmodellist"})
worker = worker.find_all("tr")
if len(worker) == lastCounts:
break
lastCounts = len(worker)
英文:
I think i found a solution using this code:
(probably not the most elegant solution - but it seems its doing what i need at the end)
import os
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
if __name__ == '__main__':
WAIT = 1
print(f"Checking Browser driver...")
os.environ['WDM_LOG'] = '0'
options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service(ChromeDriverManager().install())
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 10)
link = "https://www.appliancepartspros.com/ge-dryer-timer-knob-we1m654-ap3995088.html"
driver.get (link)
lastCounts = 0
while True:
print(f"Reading elements found {lastCounts}")
driver.execute_script("arguments[0].scrollIntoView(true);", waitWD.until(EC.presence_of_element_located((By.XPATH,'//div[@id="xmodellist"]//tr[last()]'))))
time.sleep(WAIT)
soup = BeautifulSoup (driver.page_source, 'lxml')
worker = soup.find("div", {"id": "xmodellist"})
worker = worker.find_all("tr")
if len(worker) == lastCounts:
break
lastCounts = len(worker)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论