在使用Selenium – JAVA时选择单选按钮遇到的问题。

huangapple go评论74阅读模式
英文:

Trouble selecting Radio buttons using Selenium - JAVA

问题

我正试图使用Selenium在Chrome上自动化一些测试。在选择单选按钮后进行下一步时遇到了问题。无论我尝试使用哪种方法选择单选按钮,我始终都会不断收到“NoSuchElementException”错误。以下是单选按钮的HTML代码,我正在尝试选择第一个“New (Empty)”。

<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”错误。

我尝试通过创建一个列表来选择它,如下面的线程中建议的,但由于列表为空,我得到了一个索引错误:

webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> methods = webDriver.findElements(By.name("selections"));
methods.get(0).click();
英文:

I am attempting to automate some testing using Selenium on Chrome. I am running into an issue with selecting radio buttons before moving onto the next step. I am constantly getting a 'NoSuchElementException' error with every method I try for selecting the radio button. Below is the html code that is for the radio buttons, I am trying to select the first "New (Empty)".

&lt;td&gt;
	&lt;input type=&quot;radio&quot; name=&quot;selections&quot; value=&quot;emptyAssembly&quot; id=&quot;New (Empty)&quot; onclick=&quot;onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], emptyAssembly)&quot;&gt;
	New (Empty)
	&lt;br&gt;
	&lt;input type=&quot;radio&quot; name=&quot;selections&quot; value=&quot;existingAssembly&quot; id=&quot;Existing Template, Assembly or View&quot; onclick=&quot;onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], existingAssembly)&quot;&gt;
	Existing Template, Assembly or View
	&lt;br&gt;
	&lt;input type=&quot;radio&quot; name=&quot;selections&quot; value=&quot;assemblyFile&quot; id=&quot;Assembly File&quot; onclick=&quot;onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], assemblyFile)&quot;&gt;
	Assembly File
	&lt;br&gt;
	&lt;input type=&quot;radio&quot; name=&quot;selections&quot; value=&quot;virtualDocument&quot; id=&quot;Virtual Document&quot; onclick=&quot;onSelection([emptyAssembly, existingAssembly, assemblyFile, virtualDocument], virtualDocument)&quot;&gt;
	Virtual Document
	&lt;br&gt;
&lt;/td&gt;

Below are a few of the methods I have attempted selecting it wth (the thread sleep is in there as that was a common issue people observed with radio buttons):

Thread.sleep(5000);
webDriver.findElement(By.xpath(&quot;//input[@id=&#39;New (Empty)&#39;]&quot;)).click();
Thread.sleep(5000);
webDriver.findElement(By.id(&quot;New (Empty)&quot;)).click();

I tried others but did not keep track of them, they all threw the same NoSuchElementException error.

I attempted selecting it by creating a List as suggested on other thread below, for this I get an idex error since the list contain nothing:

webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List&lt;WebElement&gt; methods = webDriver.findElements(By.name(&quot;selections&quot;));
methods.get(0).click();

答案1

得分: 0

要通过文本为 **New (Empty)** 的元素执行 `click()` 操作,可以使用以下任一 [定位策略](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver/48376890#48376890):

- `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](https://stackoverflow.com/questions/48989049/selenium-how-selenium-identifies-elements-visible-or-not-is-is-possible-that-i/48990165#48990165) 来等待 `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();
英文:

To click() on the element with text as New (Empty) you can use either of the following Locator Strategies:

  • cssSelector:

    webDriver.findElement(By.cssSelector(&quot;input[id*=&#39;Empty&#39;][value=&#39;emptyAssembly&#39;]&quot;)).click();
    
  • xpath:

    webDriver.findElement(By.xpath(&quot;//input[@value=&#39;emptyAssembly&#39; and contains(@id, &#39;Empty&#39;)]&quot;)).click();
    

However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector(&quot;input[id*=&#39;Empty&#39;][value=&#39;emptyAssembly&#39;]&quot;))).click();
    
  • xpath:

    new WebDriverWait(webDriver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath(&quot;//input[@value=&#39;emptyAssembly&#39; and contains(@id, &#39;Empty&#39;)]&quot;))).click();
    

答案2

得分: 0

尝试使用您提供的HTML代码创建一个本地的.html文件,然后在该本地HTML文件上再次尝试您的方法。我已经尝试过了,您的所有方法都能够完美地运行。

问题不在于您的方法或脚本,可能是其他原因。也许那些元素是动态的,或者在那个时候不可点击。

所以,您能提供更多细节吗?比如,情境或测试案例是什么,达到那个步骤(点击那些单选按钮)的先前步骤是什么。如果可能的话,提供一些更多的HTML代码(父级标签)和您网页的屏幕截图。

英文:

Try to create a local .html file with your provided HTML code and try your methods again on that local HTML file. I have tried and all of your methods are working perfectly.

The issue is not in your methods or script, its something else. May be those elements are dynamic or not clickable at that point.

So can you provide some more details. Like what is the scenario or test case, what are the previous steps to reach that point (clicking on those radio buttons). And If possible then provide some more HTML code (parent tags) and a screenshot of your webpage.

huangapple
  • 本文由 发表于 2020年8月18日 02:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63456357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定