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