繁体   English   中英

在Selenium Webdriver中找不到元素无线电波顿

[英]Unable to locate element radio botton in Selenium webdriver

我是一个新手,尝试使用Selenium工具学习自动化。 我正在尝试使该网站自动化-http://newtours.demoaut.com/

我登录并尝试访问此单选按钮(单向,双向)的位置,用于飞行查找器。 但我收到错误消息Unable to locate the element

尝试了以下内容。

  1. 尝试使用从firebug获得的Xpath定位元素。
  2. 使用以下由html代码组成的Xpath定位单选按钮

     //*[@type='radio']//*[@value='oneway'] //*[contains(@type,'radio')] //*[contains(text(),'oneway'] //input[@type='radio' AND @value='oneway'] 
  3. 还尝试了CSS selector来定位元素。

     driver.findElement(By.cssSelector("input[type=radio][value=oneway]")) 
  4. 尝试使用implicit waitthread.sleep添加等待时间

firebug获得的单选按钮的HTML脚本是-

input type="radio" checked="" value="roundtrip" name="tripType"

          Round Trip       

input type="radio" value="oneway" name="tripType"

              One Way

以下是我的代码-

package gurutrial2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class gurutrial2 
{

    public static WebDriver driver;

    @BeforeTest

    public final void preTest() {

        System.setProperty("webdriver.firefox.marionette", "C:/Users/serajendran/Downloads/geckodriver-0.10.0 (1)");
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setCapability("marionette", true);

        driver = new FirefoxDriver(capabilities);

        driver.get("http://newtours.demoaut.com/");
        driver.manage().window().maximize();
        System.out.println(driver.getTitle());

        Assert.assertEquals("Welcome: Mercury Tours", driver.getTitle());
    }

    @Test
    public final void login() {

        driver.findElement(By.name("userName")).sendKeys("invalidUN");
        driver.findElement(By.name("password")).sendKeys("invalidPW");
        driver.findElement(By.name("login")).click();
        System.out.println("login in progress");
    }

    @Test
    public final void flightFinder() {

        WebDriverWait wait = new WebDriverWait(driver, 30);
        WebElement oneWayRadioButton = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("oneway")));
        oneWayRadioButton.click();
        System.out.println("Clicked One Way");
    }

}

任何帮助将不胜感激。

//*[@type='radio']//*[@value='oneway'] -您要查找的是radio类型的元素,并且要取值为one.。此xpath会查找具有单值的子元素。

//*[contains(@type,'radio')] -您将获得多个结果

//*[contains(text(),'oneway'] -文本不是单向的,只有value属性是单向的,文本包含“单向”

//input[@type='radio' AND @value='oneway'] -如果将“ AND”更改为“ and”,则此方法应该有效

以下解决方案在newtour网站上为我工作-

driver.findElement(By.cssSelector("input[value='oneway']")).click();

其实问题出在您的测试方法中

TestNG中,默认情况下@Test方法的执行按字母顺序执行。 因此,在代码中,在login()之前执行的flightFinder()方法中,因此即使您使用的是右定位符单击单选按钮,它也会显示异常。

解:

  1. 保持您的方法名称按字母顺序排列

  2. @Test注释下的优先级用于方法,例如-@ @Test(priority = 1)

  3. 创建依赖性测试,例如-

     @Test() public final void login() { //code } @Test(dependsOnMethods={"login"}) public final void flightFinder() { //code } 

如下更新代码,然后尝试-

@Test
public final void doLogin() {

    driver.findElement(By.name("userName")).sendKeys("invalidUN");
    driver.findElement(By.name("password")).sendKeys("invalidPW");
    driver.findElement(By.name("login")).click();
    System.out.println("login in progress");
}

@Test()
public final void flightFinder() {

    driver.findElement(By.xpath("//input[@type='radio' and @value='oneway']")).click();
    System.out.println("Clicked One Way");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM