简体   繁体   中英

Can't find input box using Selenium python

I am trying to locate an input box using python Selenium:

 try: thisbox = driver.find_element_by_id('tbRepID') except EC.NoSuchElementException: print("Could not locate the Repair ID Box!")

Selenium is able to find the first 5 boxes using this same type of code, but for some reason, it raises the "NoSuchElementException" when trying to find the sixth one. I've tried using "find_element_by_name" and "find_element_by_id" with no success.

https://i.stack.imgur.com/nmJV6.jpg

 <table class="gray-border" cellspacing="5" cellpadding="0" width="100%" border="0"> <tbody><tr> <td colspan="4"> Part Nbr:<input name="tbPn" type="text" id="tbPn" style="width:112px;"> &nbsp;/SN:<input name="tbSn" type="text" id="tbSn" style="width:72px;"> &nbsp; or PO Nbr:<input name="tbPOnbr" type="text" id="tbPOnbr" style="width:72px;"> &nbsp; or SO Nbr:<input name="tbSOnbr" type="text" id="tbSOnbr" style="width:72px;"> &nbsp; or WO Nbr:<input name="tbWOnbr" type="text" id="tbWOnbr" style="width:72px;"> &nbsp; or Rep Id:<input name="tbRepId" type="text" id="tbRepId" style="width:56px;"> &nbsp;&nbsp; &nbsp;&nbsp; <input type="submit" name="bFind1" value="Find" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;bFind1&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="bFind1"> &nbsp;&nbsp; </td> </tr>

Without the error that you get with the sixth one, I can't really know what is the issue.

But by the description and hoping that there are no code errors, the browser could change the DOM for the page. And the driver keeps trying to find the element in the wrong DOM. I had this issue with some web pages.

The resolution I had for this was in all the interactions with an element using find the element. In Java: driver.findElement(By.id("id"));

To click the sixth box adjusant to the text or Rep Id you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#tbRepId[name='tbRepId']"))).click()
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='tbRepId' and @name='tbRepId']"))).click()
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC

Although I couldn't reference the Rep ID input box directly, I was able to reference it indirectly using:

 inputBoxes = [] inputBoxes = driver.find_elements_by_css_selector("input[type='text']") # Send repair ID inputBoxes[5].send_keys('145862')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM