英文:
MoveTargetOutOfBoundsException Selenium Python Firefox
问题
我对Selenium还不太熟悉,正在开发一个小测试脚本:
```python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
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 NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
url = "https://www.imdb.com/title/tt0368226/reviews"
options = Options()
# options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
# 加载IMDb页面
driver.get(url)
while True:
try:
button = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'load-more-trigger')))
actions = ActionChains(driver)
actions.move_to_element(button).perform()
button.click()
except NoSuchElementException:
break
我希望这个脚本能够不断点击“加载更多”按钮,直到该按钮不再出现在页面上。然而,当我运行这个脚本时,它只点击了一次按钮,然后我收到了这个错误消息:selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (479, 4602) is out of bounds of viewport width (1280) and height (827)
我查看了许多相关的Stack Overflow问题,但都没有解决我的问题。我觉得问题已经很接近了,可能是我漏看了什么。非常感谢您的帮助!
<details>
<summary>英文:</summary>
so i'm pretty new to selenium and have a little test script i'm working on:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
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 NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
url = "https://www.imdb.com/title/tt0368226/reviews"
options = Options()
options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
Load the IMDb page
driver.get(url)
while True:
try:
button = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'load-more-trigger')))
actions = ActionChains(driver)
actions.move_to_element(button).perform()
button.click()
except NoSuchElementException:
break
what i'd like for this script to do is to continuously click on the 'load more' button until it is no longer on the page. however, when i run this script, it presses the button once and then i receive this error: ``` selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (479, 4602) is out of bounds of viewport width (1280) and height (827) ```
i've checked out numerous SO questions with solutions and none have done the trick so far. i feel like this is very close and i'm just overlooking something. any help would be greatly appreciated. thanks all!
</details>
# 答案1
**得分**: 1
这里不需要使用`ActionChains`来执行对`Load More`按钮的点击。
以下是解决方案:
```python
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
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 NoSuchElementException, TimeoutException
url = "https://www.imdb.com/title/tt0368226/reviews"
options = Options()
# options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
# 加载 IMDb 页面
driver.get(url)
while True:
try:
button = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'load-more-trigger')))
button.click()
except (NoSuchElementException, TimeoutException):
break
这个while循环会不断寻找Load More
按钮并点击,直到没有更多的Load More
按钮可点击,最终会超时并退出循环。
英文:
There's no need to use ActionChains
to perform the click on the Load More
button.
Here's the solution:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
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 NoSuchElementException, TimeoutException
url = "https://www.imdb.com/title/tt0368226/reviews"
options = Options()
# options.add_argument('-headless')
driver = webdriver.Firefox(options=options)
# Load the IMDb page
driver.get(url)
while True:
try:
button = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.ID, 'load-more-trigger')))
button.click()
except (NoSuchElementException, TimeoutException):
break
The while-loop will keep looking for the Load More
button and keep clicking on it until there are no more Load More
and finally it'll get timed out and break out of the loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论