过去几天我一直在用 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 ")打开,但之后它没有做任何事情。
让我们开始清洁
pip install -U 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] 删除。
我来说两句