Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium和Python向网站发送密钥时出现的问题_Python_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

使用Selenium和Python向网站发送密钥时出现的问题

使用Selenium和Python向网站发送密钥时出现的问题,python,selenium,selenium-webdriver,xpath,css-selectors,Python,Selenium,Selenium Webdriver,Xpath,Css Selectors,目标是将用户名和密码密钥发送到网站登录页面。出于隐私原因,无法提供任何链接 为此,我尝试了Xpath和CSS选择器方法。然而,每种技术都会给我带来不同的错误 例如,使用如下所示的xpath方法 user_id =self.browser.find_elements_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']") user_id.send_keys( 'username' ) user_pw = self.browser

目标是将用户名和密码密钥发送到网站登录页面。出于隐私原因,无法提供任何链接

为此,我尝试了Xpath和CSS选择器方法。然而,每种技术都会给我带来不同的错误

例如,使用如下所示的
xpath
方法

user_id =self.browser.find_elements_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

user_pw = self.browser.find_elements_by_xpath("//*=[@id='tcl_MainContentPlaceHolder_tcl_Password']")
user_pw.send_keys( '12345678' )
user_id= WebDriverWait( self.browser, 20 ).until(EC.presence_of_element_located( (By.CSS_SELECTOR, 'tcl_MainContentPlaceHolder_tcl_Username.form-control.input-lg') ) )

user_id.send_keys( 'username' )
user_id= WebDriverWait( self.browser, 20 ).until(EC.presence_of_element_located( (By.CSS_SELECTOR, '#tcl_MainContentPlaceHolder_tcl_Username.form-control.input-lg') ) )
user_id.send_keys( 'username' )
将输出一个错误

AttributeError: 'list' object has no attribute 'send_keys'
鉴于,使用如下
CSS
选择器

user_id =self.browser.find_elements_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

user_pw = self.browser.find_elements_by_xpath("//*=[@id='tcl_MainContentPlaceHolder_tcl_Password']")
user_pw.send_keys( '12345678' )
user_id= WebDriverWait( self.browser, 20 ).until(EC.presence_of_element_located( (By.CSS_SELECTOR, 'tcl_MainContentPlaceHolder_tcl_Username.form-control.input-lg') ) )

user_id.send_keys( 'username' )
user_id= WebDriverWait( self.browser, 20 ).until(EC.presence_of_element_located( (By.CSS_SELECTOR, '#tcl_MainContentPlaceHolder_tcl_Username.form-control.input-lg') ) )
user_id.send_keys( 'username' )
将输出一个错误

selenium.common.exceptions.TimeoutException: Message: 
我使用了不同的
xpath
&
css
路径,但它并没有提供更好的结果

下面给出了每个用户名框和密码的完整html/路径

用户名

密码

如果有人能告诉我哪里做错了,我真的很感激。
提前感谢。

find\u element\u by\u xpath()
将以列表形式返回。您需要将其更改为
find\u element\u by\u xpath()
,它将以webelement形式返回

user_id =self.browser.find_element_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

对于第二个,您的css选择器是错误的。对于id,您需要在使用css选择器时添加
#


find\u element\u by\u xpath()
将以列表的形式返回。您需要将其更改为
find\u element\u by\u xpath()
,后者将以webelement的形式返回

user_id =self.browser.find_element_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

对于第二个,您的css选择器是错误的。对于id,您需要在使用css选择器时添加
#

确实会出现此错误,因为
find\u elements\u by\u xpath
正在返回一个列表。您应该尝试通过xpath查找元素

user_id =self.browser.find_elements_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
此外,Xpath不是合法的表达式,因为您没有指定要搜索的标记。请尝试以下操作:

user_id =self.browser.find_element_by_xpath("//input[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

user_pw = self.browser.find_element_by_xpath("//input[@id='tcl_MainContentPlaceHolder_tcl_Password']")
user_pw.send_keys( '12345678' )
确实会出现此错误,因为
find\u elements\u by\u xpath
正在返回一个列表。您应该尝试通过xpath查找元素

user_id =self.browser.find_elements_by_xpath("//[@id='tcl_MainContentPlaceHolder_tcl_Username']")
此外,Xpath不是合法的表达式,因为您没有指定要搜索的标记。请尝试以下操作:

user_id =self.browser.find_element_by_xpath("//input[@id='tcl_MainContentPlaceHolder_tcl_Username']")
user_id.send_keys( 'username' )

user_pw = self.browser.find_element_by_xpath("//input[@id='tcl_MainContentPlaceHolder_tcl_Password']")
user_pw.send_keys( '12345678' )

啊,感谢您指出不同的btw元素&元素和#。感谢您指出不同的btw元素&元素和#。感激