1-Selenium - 问题集
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
这个问题相对复杂,可能的原因就是鼠标要悬停的标签不在可视的窗口内,导致悬停失败。
为了证明,我本机测百度主页,悬浮到【设置】,然后获取弹出框内【搜索设置】文本。
正常代码示例
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options as chrome_options
from selenium.webdriver.firefox.options import Options as firefox_optoions
# 创建浏览器对象
firefox_options = firefox_optoions()
firefox_options.add_argument("-headless")
chrome_options = chrome_options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# driver = webdriver.Firefox(firefox_options=firefox_options)
driver = webdriver.Firefox()
# driver = webdriver.Chrome(chrome_options=chrome_options)
# driver = webdriver.Chrome()
driver.implicitly_wait(10)
action = ActionChains(driver)
try:
url = "https://www.baidu.com/"
driver.get(url)
action.move_to_element(driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')).perform()
text = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[4]/div[1]/div/a[1]').text
print(text)
except Exception as e:
print(e)
finally:
time.sleep(5)
driver.quit()
可能的报错示例(因为不同电脑可能结果不一致)
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options as chrome_options
from selenium.webdriver.firefox.options import Options as firefox_optoions
# 创建浏览器对象
firefox_options = firefox_optoions()
firefox_options.add_argument("-headless")
chrome_options = chrome_options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
# driver = webdriver.Firefox(firefox_options=firefox_options)
# driver = webdriver.Firefox()
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome()
driver.implicitly_wait(10)
action = ActionChains(driver)
try:
url = "https://www.baidu.com/"
driver.get(url)
driver.set_window_size(300, 200)
action.move_to_element(driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')).perform()
text = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[4]/div[1]/div/a[1]').text
print(text)
except Exception as e:
print(e)
finally:
time.sleep(5)
driver.quit()
无论是无头还是网页模式下,当使用默认窗口大小测试时,都不报错,这意味着鼠标悬浮的标签此时还在可视窗口内;当手动将窗口大小设置的小一些时,就报错了,这意味着鼠标悬浮的标签不在可视窗口内。
当你遇到如下几种错误(根据测试环境不同,报错不同):
Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div[1]/div[4]/div[1]/div/a[1]"}
(Session info: chrome=86.0.4240.183)
Message: (787, 30) is out of bounds of viewport width (454) and height (120)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
尤其是上述报错的最后一个,你可以尝试手动的将窗口设置到尽可能的大,这里推荐下面两个方法搭配食用,效果更佳哦:
driver.maximize_window()
driver.set_window_size(1000, 900)
这里有个套路,你先可以使用driver.maximize_window()
来尝试解决问题,如果仍然报错,稳住,这可能是driver.maximize_window()
后的窗口也不够大,导致鼠标要悬浮的标签仍然不再可视窗口内,这个时候你就要driver.get_window_size()
来获取下maximize_window
后的大小,然后再手动使用driver.set_window_size(1000, 900)
将值设置的比driver.maximize_window()
的值大,多次尝试后,应该能解决问题。
因为我自己电脑多次测试的值不一致,并且不同浏览器和不同的模式、不同的窗口大小,报错可能也不一致........所以,我只能不断尝试。而你,直奔主题,手动将窗口大小往大了设置就完了!
参考:使用selenium出现MoveTargetOutOfBoundsException异常 | [selenium]selenium.common.exceptions.MoveTargetOutOfBoundsException的处理 | Selenium - MoveTargetOutOfBoundsException with Firefox
DeprecationWarning: use options instead of chrome_options driver = webdriver.Chrome("chromedriver.exe",chrome_options=chrome_options)
弃用警告,只需要将chrome_options
改为options
即可。