Seleniumを使用したラジオボタンの選択の問題-JAVA
ChromeでSeleniumを使用していくつかのテストを自動化しようとしています。次のステップに進む前に、ラジオボタンの選択で問題が発生しています。ラジオボタンを選択しようとするすべてのメソッドで、常に「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()
、あなたは以下のいずれかの使用することができますロケータ戦略:
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コード(親タグ)とWebページのスクリーンショットを提供します。