MoveTargetOutOfBoundsException Selenium Python Firefox

huangapple go评论68阅读模式
英文:

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&#39;m pretty new to selenium and have a little test script i&#39;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&#39;d like for this script to do is to continuously click on the &#39;load more&#39; 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&#39;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&#39;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 = &quot;https://www.imdb.com/title/tt0368226/reviews&quot;

options = Options()
# options.add_argument(&#39;-headless&#39;)
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, &#39;load-more-trigger&#39;)))

        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.

huangapple
  • 本文由 发表于 2023年7月20日 11:04:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726412.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定