使用Selenium python找不到输入框

2024-09-28 01:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python Selenium查找输入框:

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

Selenium能够使用相同类型的代码找到前5个框,但由于某些原因,当试图找到第六个框时,它会引发“NoTouchElementException”。我尝试过使用“按名称查找元素”和“按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>

Tags: ortextnameid元素inputstyletype
3条回答

虽然我无法直接引用Rep ID输入框,但我可以使用以下方法间接引用它:

&13; 第13部分,;
inputBoxes = []
inputBoxes = driver.find_elements_by_css_selector("input[type='text']")

# Send repair ID
inputBoxes[5].send_keys('145862')
和#13;
和#13;

要单击文本或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
    

如果没有第六个的错误,我真的不知道问题出在哪里

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

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

相关问题 更多 >

    热门问题