如何使用http://jqueryui.com中的RadioButton Scenario的相对Xpath定位元素

问题描述 投票:0回答:5

我试图通过网站qazxsw poi自动化Selenium中的Radio Button场景

我尝试使用xss by CssSelector,自定义xpath,但没有什么对我有用。我最终不得不使用绝对xpath(虽然不是最好的做法)来使代码工作。

http://jqueryui.com/checkboxradio/
java selenium xpath webdriver webdriverwait
5个回答
1
投票

试试下面的代码。我认为webdriver点击不起作用。所以我用javascript执行器点击元素。

public class CheckBoxAndRadioButtons {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver" , "C:/Users/User/Desktop/Selenium Drivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://jqueryui.com/checkboxradio/");
        driver.manage().window().maximize();

        driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@class='demo-frame']")));
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        WebElement radiobox = driver.findElement(By.xpath("/html/body/div/fieldset[1]/label[1]/span[1]"));
        boolean isDisplayedstatus = radiobox.isDisplayed();
        System.out.println(isDisplayedstatus);
        boolean isEnabledstatus = radiobox.isEnabled();
        System.out.println(isEnabledstatus);
        boolean isSelectedstatus = radiobox.isSelected();
        System.out.println(isSelectedstatus);

        Thread.sleep(2000);
        List<WebElement> ele = driver.findElements(By.xpath("//input[@type ='radio']"));
        for(WebElement we:ele) {
            if(!we.isSelected()) {
                we.click();
            }
        }

    }
}

输出:

WebElement radiobox = driver.findElement(By.xpath("//input[@id='radio-1']"));
        boolean isDisplayedstatus = radiobox.isDisplayed();
        System.out.println(isDisplayedstatus);
        boolean isEnabledstatus = radiobox.isEnabled();
        System.out.println(isEnabledstatus);
        boolean isSelectedstatus = radiobox.isSelected();
        System.out.println(isSelectedstatus);

        List<WebElement> ele = driver.findElements(By.xpath("//input[starts-with(@id, 'radio')]"));
        for(WebElement we:ele) {
            if(!we.isSelected()) {
                JavascriptExecutor executor = (JavascriptExecutor) driver;
                executor.executeScript("arguments[0].click();",we);
                System.out.println("Clicked : " + we.getAttribute("id"));
               // we.click();
            }
        }

2
投票

如果我正确地解释了您不想使用完整xPath的问题,我发现以下内容将识别您抓取的相同无线电元素:

true
true
false
Clicked : radio-1
Clicked : radio-2
Clicked : radio-3

然后找到随后的复选框(“纽约”之后的两个),你可以使用

"//fieldset[1]/label[1]/span[1]"

"//fieldset[1]/label[2]/span[1]"

2
投票

单选按钮是一个带有无线电类型的输入元素,并且易于与之交互。为此你可以使用id "//fieldset[1]/label[3]/span[1]"

如果要按标签文本定位单选按钮,例如“纽约”,则可以使用下面的xpath:

radio-1

代码将是:

//label[normalize-space(.)='New York']/following-sibling::input[@type='radio'][1]

你也可以使用WebElement radioButton = driver.findElement(By.xpath("//label[normalize-space(.)='New York']/following-sibling::input[@type='radio'][1]"); boolean isDisplayed = radioButton.isDisplayed(); System.out.println(isDisplayed); boolean isEnabled = radioButton.isEnabled(); System.out.println(isEnabled); boolean isSelected = radioButton.isSelected(); System.out.println(isSelected); 元素来选择使用label,但//label[normalize-space(.)='New York']将无法工作,你必须检查isSelected() css类可用性:

ui-state-active

1
投票

如果您尝试选择// Using label WebElement radioButton = driver.findElement(By.xpath("//label[normalize-space(.)='New York']")); // Select radio by clicking on label radioButton.click(); // Check if radio selected by css class of the label boolean selected = radioButton.getAttribute("class").contains("ui-state-active"); 单选按钮,则可以使用以下xpath。

New York

1
投票

要单击文本New York旁边的单选按钮,您需要为//label[contains(@class,'ui-checkboxradio-radio-label') and text()='New York'] 引入WebDriverWait,您可以使用以下解决方案:

  • 代码块: elementToBeClickable()
  • 控制台输出: package demo; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class Q55111127_Chaitanya_Maligi { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.addArguments("start-maximized"); options.addArguments("--disable-extensions"); WebDriver driver = new ChromeDriver(options); driver.get("http://jqueryui.com/checkboxradio/"); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='demo-frame']"))); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='widget']//fieldset//label[@for='radio-1']"))).click(); System.out.println("Clicking of CheckBox Completed"); } }
  • 浏览器快照:

Clicking of CheckBox Completed

© www.soinside.com 2019 - 2024. All rights reserved.