PYTHON:xPath 不会定位和输入字段。使用 selenium、Webdriver 等

萨德·梅蒙

过去几天我一直在用 selenium 研究 xPath,但一直无法找到我的问题的答案。我遵循了一些关于如何在网页上使用“检查”定位文本字段的指南,然后将 send_keys() 发送到该字段。但我提出了一些错误:

import webbrowser
from selenium import webdriver
driver = webdriver.Chrome

driver = webdriver.Chrome
webbrowser.open("https://www.facebook.com/")

id_box = driver.find_element_by_xpath('//*[@id="email"]')
id_box.send_keys('username')

它响应此错误:

Traceback (most recent call last):
File "/Users/****/Library/Preferences/PyCharmCE2019.1/scratches/scratch_11.py", line 21, in <module>
id_box = driver.find_element_by_xpath('//*[@id="email"]')
TypeError: find_element_by_xpath() missing 1 required positional argument: 'xpath'

我曾尝试将我的代码更改为:

id_box = driver.find_element_by_xpath(xpath='//*[@id="email"]')

但后来它给了我这个错误:

    id_box = driver.find_element_by_xpath(xpath='//*[@id="email"]')
    TypeError: find_element_by_xpath() missing 1 required positional argument: 'self'

我已经尝试了我的小脑袋能想到的所有东西,以及谷歌等大脑袋建议的所有东西。但它不会发送密钥或识别文本框。我还尝试了其他方法,如 find_element_by_id 或 find_element_by_name,但仍然没有成功。在此之前,我尝试做一个谷歌登录脚本,并产生了相同的结果。

(Ps) 该链接确实使用 webbrowser.open(" https://www.facebook.com ")打开,但之后它没有做任何事情。

德米特里

让我们开始清洁

  1. 安装Chrome 浏览器
  2. 安装匹配版本的Chromedriver
  3. 使用pip安装最新版本selenium

    pip install -U selenium
    
  4. 使用Explicit Wait帮助 Selenium 定位元素

示例建议代码:

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

driver = webdriver.Chrome("c:\\path\\to\\chromedriver.exe")
driver.maximize_window()
driver.get("https://facebook.com")

id_box = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id ='email']")))
id_box.send_keys("username")
driver.quit()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Selenium Webdriver在Python中定位iframe

使用Java与Python的Selenium Webdriver

使用XPath在Selenium WebDriver中查找Href

使用Selenium WebDriver XPath选择组合框

Selenium Webdriver(Java)-在XPATH中使用AND / OR条件

使用TestNG的Selenium Webdriver-无法从标签中找到输入字段的正确Xpath

无法在 python selenium 中使用 xpath 和 id 定位 iframe

使用Selenium和Python无法通过xpath定位元素

无法在python中使用Selenium Webdriver定位元素

无法在 python 上使用 selenium webdriver 定位元素

Selenium Webdriver Python XPath不起作用

使用Selenium Webdriver Python上传文件

使用Selenium Python WebDriver滚动网页

在Python中使用Selenium Webdriver下载图像

使用Python Selenium Webdriver测试“临时” DIV

使用Selenium Webdriver,Python检索动态值

在 python 的单独函数中使用 Selenium、Webdriver

使用 Selenium webdriver 创建新的 python 类

无法使用Selenium WebDriver定位输入元素

如何通过Selenium Webdriver和xpath查找按钮元素

无法使用Selenium和python定位文本输入元素

我如何使用Selenium Webdriver访问XPath更改的WebElement

Selenium Webdriver:使用xpath从<td>标记获取文本

Selenium WebDriver:使用XPath在SVG中单击元素

需要在Selenium Webdriver中使用xpath选择下拉值

XPath选择器Selenium WebDriver,无法使用ID

如何使用Xpath Selenium WebDriver JAVA获取多个元素(div)?

无法使用 selenium webdriver python 中的发送键输入文本

无法使用 C# 在 selenium webdriver 脚本中使用 XPath 定位元素