首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Selenium WebElements难度

Selenium WebElements难度
EN

Stack Overflow用户
提问于 2018-06-10 02:27:18
回答 1查看 576关注 0票数 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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-10 02:52:00

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")))
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50777329

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档