不知道我在这里错过了什么。我试着用ActionChains做实验,却无法让鼠标移动。任何想法吗?
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ship2usa(driver)
time.sleep(2)
button = driver.find_element_by_xpath("//input[@class='gNO89b']")
action = webdriver.ActionChains(driver)
action.move_to_element(button).click().perform()
我正在使用pycharm, IDE不认为我在使用ActionChains库。
错误提示:
File "/home/jordan/Documents/GitHub/FMC/testcode.py", line 65, in <module>
action.move_to_element(button).click().perform()
File "/home/jordan/.local/lib/python3.8/site-packages/selenium/webdriver/common/action_chains.py", line 75, in perform
self.w3c_actions.perform()
File "/home/jordan/.local/lib/python3.8/site-packages/selenium/webdriver/common/actions/action_builder.py", line 77, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "/home/jordan/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 400, in execute
self.error_handler.check_response(response)
File "/home/jordan/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 236, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: [object HTMLInputElement] has no size and location
(Session info: chrome=95.0.4638.54)
Stacktrace:
#0 0x5626a02a3ac3 <unknown>
#1 0x56269fd7d8f8 <unknown>
#2 0x56269fd8077c <unknown>
#3 0x56269fd80576 <unknown>
#4 0x56269fd8083c <unknown>
#5 0x56269fdb9e0c <unknown>
#6 0x56269fdb9303 <unknown>
#7 0x56269fdeced3 <unknown>
#8 0x56269fdd0b02 <unknown>
#9 0x56269fde3ca1 <unknown>
#10 0x56269fdd09f3 <unknown>
#11 0x56269fda6e14 <unknown>
#12 0x56269fda7e05 <unknown>
#13 0x5626a02d525e <unknown>
#14 0x5626a02eaafa <unknown>
#15 0x5626a02d61b5 <unknown>
#16 0x5626a02ec4c8 <unknown>
#17 0x5626a02ca95b <unknown>
#18 0x5626a0307298 <unknown>
#19 0x5626a0307418 <unknown>
#20 0x5626a0322bed <unknown>
#21 0x7fc2d043e609 <unknown>
是的,我知道如何找到元素和点击不使用actionchains....测试的目的是学习如何在不那么容易的事情上使用动作链。
您需要将按钮的xpath替换为(//input[@class='gNO89b'])[2]
完整代码
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ship2usa(driver)
time.sleep(2)
button = driver.find_element_by_xpath("(//input[@class='gNO89b'])[2]")
action = webdriver.ActionChains(driver)
action.move_to_element(button).click().perform()