0

我目前正在开发一个允许直接连接到某个社交网络的程序。这是代码:

browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.get("https://www.instagram.com") 

username = "Richard"

browser.get('https://www.instagram.com')
input_username = browser.find_elements_by_xpath(
    "//input[@name='username']"
)

action=ActionChains(browser)
action.move_to_element(input_username)
action.click()
action.send_keys(username)
action.perform()

我遇到了一个问题,因为我收到了这个错误:

AttributeError: move_to requires a WebElement

我能做些什么来解决这个问题?

4

1 回答 1

2

input_username不是 WebElement,而是您使用的列表,find_elements_by_xpath()而不是find_element_by_xpath().

尝试任一

input_username = browser.find_element_by_xpath("//input[@name='username']")
action.move_to_element(input_username)

或者

input_username = browser.find_elements_by_xpath("//input[@name='username']")
action.move_to_element(input_username[0])

请注意,身份验证表单由 JavaScript 动态生成,因此要能够处理输入字段,您需要等待 JavaScript 执行:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

input_username = wait(browser, 10).until(EC.element_to_be_clickable((By.NAME, "username")))
于 2018-06-09T18:51:59.723 回答