Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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 无法单击位于不同帧中的单选按钮_Java_Google Chrome_Selenium_Selenium Webdriver_Automation - Fatal编程技术网

Java 无法单击位于不同帧中的单选按钮

Java 无法单击位于不同帧中的单选按钮,java,google-chrome,selenium,selenium-webdriver,automation,Java,Google Chrome,Selenium,Selenium Webdriver,Automation,我正在尝试单击位于不同帧(不在父帧)中的单选按钮。当我执行下面的代码时,没有例外,但令人惊讶的是,它甚至没有点击按钮。如果您有任何想法/在我的代码中发现任何问题,请告诉我 import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa

我正在尝试单击位于不同帧(不在父帧)中的单选按钮。当我执行下面的代码时,没有例外,但令人惊讶的是,它甚至没有点击按钮。如果您有任何想法/在我的代码中发现任何问题,请告诉我

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Practise_Radio_Checkbox {

    public static void main(String[] args) throws InterruptedException {

         System.setProperty("webdriver.chrome.driver", "D:\\Learning\\API\\Selenium\\chromedriver.exe");


            WebDriver browser = new ChromeDriver();

            browser.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS);

            browser.manage().window().maximize();


            browser.get("http://www.quackit.com/html/codes/html_radio_button.cfm");

            WebElement e1 = browser.findElement(By.name("result2"));


            browser.switchTo().frame(e1);

            browser.findElement(By.xpath("html/body/form/input[1]")).click();

            browser.quit();


    }

}

我可以点击单选按钮,这是在不同的框架比家长。我认为问题是,您试图按名称切换帧“-
findelelement(按.Name(“result2”))”
。我用Xpath试过了。它对我有效,我可以点击单选按钮“红色”

以下是工作代码:-

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class iframeradiobutton {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.quackit.com/html/codes/html_radio_button.cfm");

        //Switch to Iframe
        WebElement iframe = driver.findElement(By.xpath("/html/body/div[1]/div/article/div[2]/div[2]/div[2]/iframe"));
        driver.switchTo().frame(iframe);
        driver.findElement(By.xpath("/html/body/form/input[1]")).click();
        System.out.println("Clicked on Radio Button Red");
        driver.switchTo().defaultContent();// Iframe is Switched to Main Again

        driver.close(); // Closes the current driver instance. 
    }
}
输出以供参考

我可以单击单选按钮,它与父项位于不同的帧中。我认为问题是,您试图按名称切换帧“-
findelelement(按.Name(“result2”))”
。我用Xpath试过了。它对我有效,我可以点击单选按钮“红色”

以下是工作代码:-

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class iframeradiobutton {
    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.quackit.com/html/codes/html_radio_button.cfm");

        //Switch to Iframe
        WebElement iframe = driver.findElement(By.xpath("/html/body/div[1]/div/article/div[2]/div[2]/div[2]/iframe"));
        driver.switchTo().frame(iframe);
        driver.findElement(By.xpath("/html/body/form/input[1]")).click();
        System.out.println("Clicked on Radio Button Red");
        driver.switchTo().defaultContent();// Iframe is Switched to Main Again

        driver.close(); // Closes the current driver instance. 
    }
}
输出以供参考

首先,它可以在Firefox上运行(即使没有任何明确的等待):

在Chrome上,出于未知原因,您必须通过javascript进行点击:

此外,我还必须添加一个显式等待,以等待帧出现:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement iframe = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("result2")));
driver.switchTo().frame(iframe);

首先,它适用于Firefox(即使没有任何显式等待):

在Chrome上,出于未知原因,您必须通过javascript进行点击:

此外,我还必须添加一个显式等待,以等待帧出现:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement iframe = wait.until(ExpectedConditions.presenceOfElementLocated(By.name("result2")));
driver.switchTo().frame(iframe);

你好我已经检查了下面的代码,它是为我工作。请检查一下,让我知道。谢谢你好我已经检查了下面的代码,它是为我工作。请检查一下,让我知道。谢谢感谢您的时间:)虽然听起来很奇怪,但代码正在执行Print语句并在控制台中打印;但是我没有看到单选按钮被选中。还没有检查。为了避免混淆,我删除了driver.close()语句,以便浏览器窗口保持打开状态,但没有效果。是点击你的系统,选择是可见的吗?嗨,普拉博德-请你检查一下,你使用的代码与上面相同。因为它在我这边很好用。我已附上屏幕截图供您参考,我可以选择“红色”单选按钮。请向我提供相同的最新信息。我看到您的代码在您的系统中工作,但不是使用相同的代码,而是不工作。除了你的代码之外,我做的唯一不同的事情就是在IE和Chrome中运行测试(这两种情况都没有运气),因为FF和Webdriver存在兼容性问题。如果你不介意的话,你能在IE和/或Chrome中运行你的代码吗?@PrabodhGhosh我实际上能够在Chrome中可靠地重现你的问题。有趣的问题!谢谢@alecxe,至少我能分享我的痛苦。。。以下是我为解决这个问题所做的事情:1。杀死所有ChromeDriver会话,2。切换到第3帧时,使用了框架参数的所有三种形式(id、字符串、web元素)。尝试与页面中的其他元素交互,并成功。现在我将尝试与其他站点的单选按钮进行交互;如果我成功了,那么HTML肯定有问题;否则,我就做错了。感谢您的时间:)虽然听起来可能很奇怪,但代码正在执行Print语句并在控制台中打印;但是我没有看到单选按钮被选中。还没有检查。为了避免混淆,我删除了driver.close()语句,以便浏览器窗口保持打开状态,但没有效果。是点击你的系统,选择是可见的吗?嗨,普拉博德-请你检查一下,你使用的代码与上面相同。因为它在我这边很好用。我已附上屏幕截图供您参考,我可以选择“红色”单选按钮。请向我提供相同的最新信息。我看到您的代码在您的系统中工作,但不是使用相同的代码,而是不工作。除了你的代码之外,我做的唯一不同的事情就是在IE和Chrome中运行测试(这两种情况都没有运气),因为FF和Webdriver存在兼容性问题。如果你不介意的话,你能在IE和/或Chrome中运行你的代码吗?@PrabodhGhosh我实际上能够在Chrome中可靠地重现你的问题。有趣的问题!谢谢@alecxe,至少我能分享我的痛苦。。。以下是我为解决这个问题所做的事情:1。杀死所有ChromeDriver会话,2。切换到第3帧时,使用了框架参数的所有三种形式(id、字符串、web元素)。尝试与页面中的其他元素交互,并成功。现在我将尝试与其他站点的单选按钮进行交互;如果我成功了,那么HTML肯定有问题;否则,我就做错了。