python - 无法在 selenium 上找到部分 ID

标签 python selenium selenium-webdriver

大家好!

我正在尝试定位具有动态 ID 的元素,但是!无论我做什么,除了 NoSuchElementException 都会系统地引发......

这是我的代码:

try:
    driver.get('http://www.website.com/')
    usr = driver.find_element_by_css_selector("a[id*='txtEmail']")
    pwd = driver.find_element_by_css_selector("a[id*='txtPassword']")

    usr.send_keys(username)
    pwd.send_keys(password)
    driver.find_element_by_css_Selector("a[id*='btnLogin']").click()

    if (driver.find_element_by_css_Selector("a[id$=lnkLogOut]")):
            log.info("Successfully connected")
    else:
            log.error("Login failed - Cannot login to the website - Wrong username/password ?")

    except NoSuchElementException:
            log.error("Cannot find element needed on the login page")

    except TimeoutException:
            log.error("Cannot reach the website in time")
    except:
            log.error("Error occurred during the login attempt: {}".format(sys.exc_info()))

有html格式:

<td>Email/Username:</td>
<td><input name="ctl00$cph1$Login1$txtEmail" id="ctl00_cph1_Login1_txtEmail" style="width:160px;" type="text"></td>

<td>Password:</td>
<td><input name="ctl00$cph1$Login1$txtPassword" id="ctl00_cph1_Login1_txtPassword" style="width:160px;" type="password"></td>
<td><input name="ctl00$cph1$Login1$btnLogin" value="Login" id="ctl00_cph1_Login1_btnLogin" class="fancy" with="80px;" style="height:28px;" type="submit"></td>  

还有我的输出错误:

DEBUG:selenium.webdriver.remote.remote_connection:POST 

http://127.0.0.1:40943/hub/session {"desiredCapabilities": {"platform": "ANY", "browserName": "firefox", "version": "", "marionette": false, "javascriptEnabled": true}}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:40943/hub/session/2b64c16e-1582-42ee-8041-66550781c00f/url {"url": "http://www.website.com", "sessionId": "2b64c16e-1582-42ee-8041-66550781c00f"}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
DEBUG:selenium.webdriver.remote.remote_connection:POST http://127.0.0.1:40943/hub/session/2b64c16e-1582-42ee-8041-66550781c00f/element {"using": "css selector", "sessionId": "2b64c16e-1582-42ee-8041-66550781c00f", "value": "a[id*='txtEmail']"}
DEBUG:selenium.webdriver.remote.remote_connection:Finished Request
ERROR:__main__:Cannot find element needed on the login page

问题出在哪里?有什么想法吗?

编辑:以下行引发错误:

usr = driver.find_element_by_css_selector("a[id*='txtEmail']")
pwd = driver.find_element_by_css_selector("a[id*='txtPassword']")

最佳答案

要遵循@murali 关于ainput 的观点,您只需修复您的 CSS 选择器即可:

usr = driver.find_element_by_css_selector("input[id*=txtEmail]")
pwd = driver.find_element_by_css_selector("input[id*=txtPassword]")

您还可以使用“ends-with”检查而不是“contains”:

usr = driver.find_element_by_css_selector("input[id$=txtEmail]")
pwd = driver.find_element_by_css_selector("input[id$=txtPassword]")

关于python - 无法在 selenium 上找到部分 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36662934/

相关文章:

python - rabbitmq 队列填满了 celery 任务

Python if、elif、else 当 if 不为 true 时出现问题跳过

java - Mockito:WAITING与参数匹配的调用

java - position() 函数给我带来了错误的数据

selenium - 在 selenium : The constructor PDFParser(BufferedInputStream) is undefined 中阅读 PDF

java - 使用 Selenium Webdriver 运行基于 Web 的 SSH CLIENT PLUGIN 脚本的自动化

python - Selenium 在页面加载时覆盖 javascript

python - 我可以指定在 .coveragerc 中运行哪些测试文件吗?

python - 无法使用selenium python找到信用卡号码的元素

java - 我们如何使用 selenium webdriver 获得加载所有链接的准确时间?