Cannot locate a partial id on selenium

浪子不回头ぞ 提交于 2020-05-13 09:45:34

问题


Hi everyone !

I am trying to locate an element with a dynamic ID, but ! whatever i do, the except NoSuchElementException is systematically raised...

There is my code :

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()))

There is the html form:

<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>  

And there is my output error:

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

Where's the problem ? Any idea ?

EDIT : The following lines provoke the error:

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

回答1:


To follow the @murali's point about the a versus input, you can just fix your CSS selectors:

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

You can also use the "ends-with" check instead of "contains":

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



回答2:


As per provided HTML code tags for all means username, password and submit buttons are 'input' but in code used 'a' . so that is the reason for the issue.

As per comment following one worked

  usr = driver.find_elements_by_xpath('//*[contains(@id, "txtEmail")]') 

because here tag 'a' is not used in xpath instead '*' is used meany any tag so input here.

I am expecting logout button also have tag 'input' as HTML code for logout element is not provided. so please check that and create correct css or xpath so that time out exception will be resolved.

Thank You, Murali



来源:https://stackoverflow.com/questions/36662934/cannot-locate-a-partial-id-on-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!