简体   繁体   English

使用Selenium python找不到输入框

[英]Can't find input box using Selenium python

I am trying to locate an input box using python Selenium:我正在尝试使用 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. Selenium 能够使用相同类型的代码找到前 5 个框,但由于某种原因,它在尝试找到第六个框时会引发“NoSuchElementException”。 I've tried using "find_element_by_name" and "find_element_by_id" with no success.我试过使用“find_element_by_name”和“find_element_by_id”但没有成功。

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.但是根据描述并希望没有代码错误,浏览器可以更改页面的 DOM。 And the driver keeps trying to find the element in the wrong DOM.驱动程序不断尝试在错误的 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.我对此的解决方案是使用 find 元素与元素进行所有交互。 In Java: driver.findElement(By.id("id"));在 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:要单击与文本或 Rep Id匹配的第六个框,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一解决方案:

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#tbRepId[name='tbRepId']"))).click()
  • Using XPATH :使用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:虽然我无法直接引用 Rep ID 输入框,但我可以使用以下方法间接引用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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