python - Selenium WebElements 难度

标签 python html selenium

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

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

我可以做什么来解决这个问题?

最佳答案

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")))

关于python - Selenium WebElements 难度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777329/

相关文章:

javascript - iPad 2 上的 Safari 动态加载图像导致内存泄漏

c# - OpenQA.Selenium.DriverServiceNotFoundException

java - Selenium 3.0.1 - 交互式给出 ParameterException : Unknown option: -interactive

python - 如何优先考虑正则表达式模式

python - 根据单个列表的排序对python中的多个列表进行排序

javascript - 游戏计数器不增加 - Javascript

html - 如何在小屏幕上内联显示字段集文本和按钮?

java - 如何使用 Maven 运行 Selenium 测试?

Python HTTP 请求和调试级别记录到日志文件

python - 云中的 OpenCV