问题:
使用selenium库时报错:MoveTargetOutOfBoundsException: Message: move target out of bounds
修改前代码:
from selenium import webdriver
driver = webdriver.Chrome(executable_path = r'D:\Chrome Chrome\Google\Chrome\Application\chromedriver.exe') #driver的路径
driver.get("http://edu.cstor.cn/#") #打开网站
ac = driver.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div/div/span[1]') #元素的XPath
ActionChains(driver).move_to_element(ac).click(ac).perform() #将鼠标移动到元素位置并单击
原因:
要寻找的元素不在视野范围内
解决方法:
把窗口最大化,用driver.maximize_window()
修改后代码:
(修改在第4行)
from selenium import webdriver
driver = webdriver.Chrome(executable_path = r'D:\Chrome Chrome\Google\Chrome\Application\chromedriver.exe')#driver的路径
driver.get("http://edu.cstor.cn/#") #打开网站
driver.maximize_window() #窗口最大化(不然找不到"登录")
ac = driver.find_element_by_xpath('//*[@id="app"]/div/div[1]/div/div/div/div/span[1]') ##元素的XPath
ActionChains(driver).move_to_element(ac).click(ac).perform() #将鼠标移动到某个元素的位置并单击