Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法选择单选按钮";NoTouchElementException:没有这样的元素:无法定位元素“;显示错误_Java_Selenium Webdriver_Selenium Chromedriver_Browser Automation_Webautomation - Fatal编程技术网

Java 无法选择单选按钮";NoTouchElementException:没有这样的元素:无法定位元素“;显示错误

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

我试图从给定的3个单选按钮中选择一个单选按钮,但没有得到这样的元素异常

以下是所有3个单选按钮的HTML代码:

<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>

通常,当这种情况发生时,原因是两个原因之一

  • XPath不正确
  • 您需要向driver.findElement语句添加等待

  • (1) 检查XPath是否正确

  • 打开Chromedriver并转到您正在测试的网站
  • 按F12键打开控制台(打开后,单击“控制台”选项卡)
  • 在Chrome浏览器控制台中键入以下内容,查看是否返回任何对象
  • $x(“//img[contains(@alt,'AltText2')]”)
  • (2) 等待元素可单击

    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");