[英]How can I define send_keys for python function?
I need advice on how to call send_keys for user input.我需要关于如何为用户输入调用 send_keys 的建议。 If I assign a variable for the line self.browser.find_elements_by_id ("Login1_UserName")
and then send it to send_keys, the solution does not work.如果我为self.browser.find_elements_by_id ("Login1_UserName")
行分配一个变量,然后将其发送到 send_keys,则该解决方案不起作用。 What am I doing wrong?我究竟做错了什么?
def login(Self):
# login to the app
username = self.browser.find_elements_by_id ("Login1_UserName")
username.send_keys ("userone")
find_elements_*
would return a List and you can't invoke send_keys()
on a List . find_elements_*
将返回一个List并且您不能在List上调用send_keys()
。 So you need to replace find_elements_*
with find_element_*
and you can use the following Locator Strategies :所以,你需要更换find_elements_*
与find_element_*
您可以使用下面的定位策略:
def login(Self):
# login to the app
self.browser.find_element_by_id("Login1_UserName").send_keys("userone")
As per best practices, while invoking send_keys()
you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following solutions:根据最佳实践,在调用send_keys()
您需要为element_to_be_clickable()
引入WebDriverWait ,您可以使用以下任一解决方案:
Using ID
:使用ID
:
WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.ID, "Login1_UserName"))).send_keys("Tomasito")
Using CSS_SELECTOR
:使用CSS_SELECTOR
:
WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#Login1_UserName"))).send_keys("Tomasito")
Using XPATH
:使用XPATH
:
WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='Login1_UserName']"))).send_keys("Tomasito")
Note : You have to add the following imports :注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
This is because you have used find_elements_by_id("Login1_UserName")
it will return list NOT the element.You should use find_element_by_id("Login1_UserName")
这是因为您使用了find_elements_by_id("Login1_UserName")
它将返回列表而不是元素。您应该使用find_element_by_id("Login1_UserName")
def login(Self):
# login to the app
username = self.browser.find_element_by_id("Login1_UserName")
username.send_keys("userone")
Try this code see if this work.试试这个代码看看这是否有效。
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("path of chrome driver")
driver.get('url')
username=WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'Login1_UserName')))
username.send_keys("userone")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.