查找具有更改类名的div(selenium)

2024-06-08 01:24:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在网上学习硒课程,我们正在自动登录。讲座已经过时了,我们使用的网站也更新了(Quora)。在

问题是,输入字段有随机生成的类名,这使得“find_element_id方法变得无用。在

我不知道如何登录不同的会话。如何在不同的会话中选择这些字段(具有不同的类名)。这就是我的代码。提前谢谢你。我试着问过课程指导老师,但他整个星期都没有答复。在

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome(executable_path = r'/Users/mpbro17/Desktop/chromedriver')

driver.get('https://www.quora.com')

driver.find_element_by_id("__w2_fy0VXDC_email").clear()
driver.find_element_by_id("__w2_fy0VXDC_email").send_keys("programmingdude183@gmail.com")
driver.find_element_by_id("__w2_fy0VXDC_password").clear()
driver.find_element_by_id("__w2_fy0VXDC_password").send_keys("testpassword1")
driver.find_element_by_id("__w2_fy0VXDC_submit_button").click()

错误

^{pr2}$

Tags: fromimportidbydriverseleniumelementcommon
3条回答

请尝试使用css选择器:

driver.find_element_by_css_selector('[id$="email"]')
driver.find_element_by_css_selector('[id$="password"]')
driver.find_element_by_css_selector('[id$="submit_button"]')

你必须以名字为基础标准的。但是如果按名称属性,则存在两个元素。在

enter image description here

所以按照下面的xpath

//div[@class='regular_login']//input[@name='email']
//div[@class='regular_login']//input[@name='password']
//div[@class='regular_login']//input[contains(@class,'submit')]

您可以使用find_element_by_name方法来查找您的元素(电子邮件和密码)和csselector for Login按钮。我想,这不会让你灰心。在

driver.find_element_by_name("email").clear()
driver.find_element_by_name("email").send_keys("programmingdude183@gmail.com")
driver.find_element_by_name("password").clear()
driver.find_element_by_name("password").send_keys("testpassword1")
driver.find_element_by_css_selector("input[value='Login']").click()

相关问题 更多 >

    热门问题