ปัญหาในการเลือกปุ่มวิทยุโดยใช้ Selenium - JAVA
ฉันกำลังพยายามทำการทดสอบบางอย่างโดยอัตโนมัติโดยใช้ Selenium บน Chrome ฉันประสบปัญหาในการเลือกปุ่มตัวเลือกก่อนที่จะไปยังขั้นตอนต่อไป ฉันได้รับข้อผิดพลาด 'NoSuchElementException' อยู่ตลอดเวลาในทุก ๆ วิธีที่ฉันพยายามเลือกปุ่มตัวเลือก ด้านล่างนี้คือรหัส html สำหรับปุ่มตัวเลือกฉันกำลังพยายามเลือก "ใหม่ (ว่างเปล่า)" อันแรก
<td>
<input type="radio" name="selections" value="emptyAssembly" id="New (Empty)" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], emptyAssembly)">
New (Empty)
<br>
<input type="radio" name="selections" value="existingAssembly" id="Existing Template, Assembly or View" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], existingAssembly)">
Existing Template, Assembly or View
<br>
<input type="radio" name="selections" value="assemblyFile" id="Assembly File" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], assemblyFile)">
Assembly File
<br>
<input type="radio" name="selections" value="virtualDocument" id="Virtual Document" onclick="onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], virtualDocument)">
Virtual Document
<br>
</td>
ด้านล่างนี้เป็นวิธีการบางอย่างที่ฉันได้ลองเลือกด้วย (การนอนหลับของเธรดอยู่ในนั้นเนื่องจากเป็นปัญหาทั่วไปที่ผู้คนสังเกตเห็นด้วยปุ่มตัวเลือก):
Thread.sleep(5000);
webDriver.findElement(By.xpath("//input[@id='New (Empty)']")).click();
Thread.sleep(5000);
webDriver.findElement(By.id("New (Empty)")).click();
ฉันลองใช้คนอื่น แต่ไม่ได้ติดตามพวกเขาทั้งหมดมีข้อผิดพลาด NoSuchElementException เดียวกัน
ฉันพยายามเลือกโดยสร้างรายการตามที่แนะนำในเธรดอื่นด้านล่างสำหรับสิ่งนี้ฉันได้รับข้อผิดพลาด idex เนื่องจากรายการไม่มีอะไรเลย:
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = webDriver.findElements(By.name("selections"));
methods.get(0).click();
คำตอบ
การclick()
ในองค์ประกอบที่มีข้อความเป็นใหม่ (ว่างเปล่า)คุณสามารถใช้อย่างใดอย่างหนึ่งดังต่อไปนี้กลยุทธ์ส :
cssSelector
:webDriver.findElement(By.cssSelector("input[id*='Empty'][value='emptyAssembly']")).click();
xpath
:webDriver.findElement(By.xpath("//input[@value='emptyAssembly' and contains(@id, 'Empty')]")).click();
อย่างไรก็ตามเนื่องจากองค์ประกอบนั้นเป็นองค์ประกอบแบบไดนามิกดังนั้นclick()
ในองค์ประกอบคุณจึงต้องกระตุ้นWebDriverWaitสำหรับelementToBeClickable()
และคุณสามารถใช้กลยุทธ์ Locatorอย่างใดอย่างหนึ่งต่อไปนี้:
cssSelector
:new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[id*='Empty'][value='emptyAssembly']"))).click();
xpath
:new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@value='emptyAssembly' and contains(@id, 'Empty')]"))).click();
ลองสร้างไฟล์. html ในเครื่องด้วยโค้ด HTML ที่คุณให้มาและลองใช้วิธีการของคุณอีกครั้งกับไฟล์ HTML ในเครื่องนั้น ฉันได้ลองแล้วและวิธีการทั้งหมดของคุณทำงานได้อย่างสมบูรณ์
ปัญหาไม่ได้อยู่ในวิธีการหรือสคริปต์ของคุณ แต่เป็นอย่างอื่น อาจเป็นเพราะองค์ประกอบเหล่านั้นเป็นแบบไดนามิกหรือไม่สามารถคลิกได้ในขณะนั้น
คุณช่วยให้รายละเอียดเพิ่มเติมได้ไหม เช่นเดียวกับสถานการณ์หรือกรณีทดสอบอะไรคือขั้นตอนก่อนหน้าเพื่อไปถึงจุดนั้น (คลิกที่ปุ่มตัวเลือกเหล่านั้น) และหากเป็นไปได้ให้ระบุโค้ด HTML เพิ่มเติม (แท็กหลัก) และภาพหน้าจอของหน้าเว็บของคุณ