Java 无法选择单选按钮";NoTouchElementException:没有这样的元素:无法定位元素“;显示错误
我试图从给定的3个单选按钮中选择一个单选按钮,但没有得到这样的元素异常 以下是所有3个单选按钮的HTML代码:Java 无法选择单选按钮";NoTouchElementException:没有这样的元素:无法定位元素“;显示错误,java,selenium-webdriver,selenium-chromedriver,browser-automation,webautomation,Java,Selenium Webdriver,Selenium Chromedriver,Browser Automation,Webautomation,我试图从给定的3个单选按钮中选择一个单选按钮,但没有得到这样的元素异常 以下是所有3个单选按钮的HTML代码: <div class="radioGroup"> <div> <div class="radio"> <div class="radioVal"></div> <label><span><img src="https://s3.amazonaws.com/x
<div class="radioGroup">
<div>
<div class="radio">
<div class="radioVal"></div>
<label><span><img src="https://s3.amazonaws.com/xyz.png" alt="AltText1">Radio Button1</span></label>
</div>
<div class="radio">
<div class="radioVal"></div>
<label><span><img src="https://s3.amazonaws.com/xyz1.png" alt="AltText2">Radio Button2</span></label>
</div>
<div class="radio">
<div class="radioVal"></div>
<label><span><img src="https://s3.amazonaws.com/xyz3.png" alt="AltText3">Radio Button3</span></label>
</div>
</div>
通常,当这种情况发生时,原因是两个原因之一
(1) 检查XPath是否正确
System.TimeSpan timeToWait = new TimeSpan(0, 0, 10);
WebDriverWait wait = new WebDriverWait(driver, timeToWait);
IWebElement htmlElementOnPage = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//img[contains(@alt, 'AltText2')]")));
此外,我在html中没有看到任何单选按钮。但我猜你实际上并不想点击标签,而是想点击标签上方的标签 如果这是正确的,xpath应该指向div,而不是div旁边的标签
By.Xpath("//img[contains(@alt, 'AltText2')]/../div");
你加了等待吗?是否查看元素是否在IFRAME中?问题是我使用的是隐式等待。添加显式等待该步骤后,它正在成功运行。
By.Xpath("//img[contains(@alt, 'AltText2')]/../div");