Mat*_*eus 8 python firefox selenium python-2.7 selenium-webdriver
我在Firefox Webdriver上的move_to_element 函数遇到问题(Chrome,IE运行良好)
driver = webdriver.Firefox()
driver.get("https://stackoverflow.com")
time.sleep(5)
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
ActionChains(driver).move_to_element(source_element).perform()
Run Code Online (Sandbox Code Playgroud)
我正在使用以下版本:geckodriver-0.17.0 // Firefox-54.0 //硒-3.4.3
运行此脚本后,在输出中显示:
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of viewport width (1268) and height (854)
Run Code Online (Sandbox Code Playgroud)
这个错误...
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: (134.96666717529297, 8682.183013916016) is out of bounds of Viewport width (1268) and height (854)
Run Code Online (Sandbox Code Playgroud)
...暗示您要查找的元素不在Viewport 内。我们需要向下滚动以将元素引入Viewport。这是工作代码:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://stackoverflow.com")
last_height = driver.execute_script("return document.body.scrollHeight")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
ActionChains(driver).move_to_element(source_element).perform()
Run Code Online (Sandbox Code Playgroud)
如果这能回答您的问题,请告诉我。
我认为这里的正确答案很幸运,因为他们正在寻找的元素恰好位于页面底部,并且并没有真正解释为什么这种情况通常在Firefox中发生。
Firefox以外的浏览器将Webdrivers move_to_element动作视为滚动到带有元素的页面部分,然后将其悬停在其上。Firefox似乎采取了强硬立场,即move_to_element只是悬停,正在等待滚动操作来解决此问题。
现在,您必须使用上一个答案中提到的javascript来解决此错误,但是我建议使用类似的方法而不是随意滚动(滚动至页面底部)(希望我的示例是页脚),并希望对象仍在视图中。
def scroll_shim(passed_in_driver, object):
x = object.location['x']
y = object.location['y']
scroll_by_coord = 'window.scrollTo(%s,%s);' % (
x,
y
)
scroll_nav_out_of_way = 'window.scrollBy(0, -120);'
passed_in_driver.execute_script(scroll_by_coord)
passed_in_driver.execute_script(scroll_nav_out_of_way)
Run Code Online (Sandbox Code Playgroud)
然后再
source_element = driver.find_element_by_xpath('//*[@id="footer"]/div/ul/li[1]/a')
if 'firefox' in driver.capabilities['browserName']:
scroll_shim(driver, source_element)
# scroll_shim is just scrolling it into view, you still need to hover over it to click using an action chain.
actions = ActionChains(driver)
actions.move_to_element(source_element)
actions.click()
actions.perform()
Run Code Online (Sandbox Code Playgroud)
在 Firefox 中自动化脚本时,您可以尝试以下操作,因为它通常会抛出 MoveTargetOutOfBoundsException 错误:
可以通过以下方式进行变换/放大或缩小
driver.execute_script("document.body.style.transform='scale(0.9)';")
Run Code Online (Sandbox Code Playgroud)
有时,如果您在 Jenkins(CI 工具)中运行自动化脚本,您可能还会面临上述转换代码的问题,其中浏览器的内容被扩展而不是实际的浏览器,在这种情况下您可以尝试调整浏览器窗口的大小:
driver.set_window_size(x, y)
Run Code Online (Sandbox Code Playgroud)
或者
driver.set_window_size(2000, 694)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12885 次 |
| 最近记录: |