繁体   English   中英

使用Selenium python找不到输入框

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

我正在尝试使用 python Selenium 定位输入框:

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

Selenium 能够使用相同类型的代码找到前 5 个框,但由于某种原因,它在尝试找到第六个框时会引发“NoSuchElementException”。 我试过使用“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>

如果没有你在第六个中得到的错误,我真的不知道是什么问题。

但是根据描述并希望没有代码错误,浏览器可以更改页面的 DOM。 驱动程序不断尝试在错误的 DOM 中查找元素。 我在某些网页上遇到了这个问题。

我对此的解决方案是使用 find 元素与元素进行所有交互。 在 Java 中: driver.findElement(By.id("id"));

要单击与文本或 Rep Id匹配的第六个框,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

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

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='tbRepId' and @name='tbRepId']"))).click()
  • 注意:您必须添加以下导入:

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

虽然我无法直接引用 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