首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Python和Selenium库与深度嵌套的“input”html对象交互

如何使用Python和Selenium库与深度嵌套的“input”html对象交互
EN

Stack Overflow用户
提问于 2019-06-05 23:43:09
回答 1查看 261关注 0票数 1

我正在尝试使用selenium web驱动程序python库与用户名和密码html输入对象进行交互。虽然我可以与大多数html标签交互,但我的代码不能在一个深度嵌套的“input”标签上输入用户名和密码。请参阅附件中的图像

我尝试过单独使用xpath模块,也尝试过使用WebDriverWait,以防在访问之前需要加载元素。当我尝试WebDriverWait时,代码永远不会到达timeoutException,它只是在运行时终端中冻结,我必须手动终止它。

代码语言:javascript
复制
chrome_options = Options()
#chrome_options.add_argument("--headless")
recollect_url = r"https://manage.recollect.net/admin"
driver = webdriver.Chrome("C:\Users\Jlong\Downloads\chromedriver_win32\chromedriver.exe",chrome_options=chrome_options)
driver.get(recollect_url)
pagesource = driver.page_source


try:
    myElem = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//input[@name='email']")))
    myElem2 = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'auth0-lock-input-block '
                                                                                            'auth0-lock-input-email')))

    print "Page is ready!"

except TimeoutException:

    print "Loading took too much time!"

我希望能够使用用户名和密码的send_keys()方法,然后使用提交时单击方法输入凭据

EN

回答 1

Stack Overflow用户

发布于 2019-06-05 23:56:02

使用WebdriverWait和下面的xpath。

代码语言:javascript
复制
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

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='auth0-lock-input'][@name='email']"))).send_keys('xyz@gmail.com')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='auth0-lock-input'][@name='password']"))).send_keys('testuser')

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@class='auth0-lock-submit']//span[@class='auth0-label-submit'][contains(.,'Log In')]"))).click()

输出:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56463848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档