硒点击问题



下面代码的最后一行不执行单击。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path=r"C:UsersSohaibWaseemPycharmProjectschromedriver.exe")
driver.get("https://www.facebook.com/")
user_name = driver.find_element_by_name("email") ###... checking user name or email box is available in page
print(user_name.is_displayed())      ### ... return true false of the status
print(user_name.is_enabled())        ### ... return true false of the status
pass_word = driver.find_element_by_name("pass")
print(pass_word.is_displayed())      ### ... return true false of the status
print(pass_word.is_enabled())        ### ... return true false of the status
user_name.send_keys("asdf")
pass_word.send_keys("asdf")
driver.find_element_by_xpath("Log In").click()

您定位的"登录"按钮的 xpath 应该是//input[@id="u_0_b"]而不是Log In现在,上面代码的最后一行将如下所示:

driver.find_element_by_xpath("//input[@id="u_0_b"]").click()

您用于单击登录按钮Xpath,看起来不像 xpath。IDdynamic用于登录按钮,并且ID格式与页面上其他inputs field相同,因此partial ID match不起作用。

您可以在data-testid属性的帮助下使用CSSXpath

.CSS

driver.find_element_by_css_selector("input[data-testid='royal_login_button']").click()

Xpath

driver.find_element_by_xpath("//input[@data-testid='royal_login_button']").click()

非常感谢,现在我有另一个错误 selenium.common.exceptions.StaleElementReferenceException: 消息: 过时的元素引用: 元素未附加到页面文档

''' 从硒导入网络驱动程序

from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome(executable_path=r"C:UsersSohaib 
WaseemPycharmProjectschromedriver.exe")
driver.get("https://www.facebook.com/")
user_name = driver.find_element_by_name("email") 
print(user_name.is_displayed())     
print(user_name.is_enabled())        
pass_word = driver.find_element_by_name("pass")
print(pass_word.is_displayed())      
print(pass_word.is_enabled())        
user_name.send_keys("asdf")
pass_word.send_keys("asdf")
driver.find_element_by_id("loginbutton").click()
time.sleep(2)
driver.back()
user_name.send_keys("my.email@gmail.com")
pass_word.send_keys("My password")
driver.find_element_by_id("loginbutton").click()

'''

我已经尝试了很多例外,但没有结果

相关内容

  • 没有找到相关文章

最新更新