Java 在Selenium webdriver中找不到元素radio botton
我是一个新手,尝试使用工具Java 在Selenium webdriver中找不到元素radio botton,java,selenium,xpath,Java,Selenium,Xpath,我是一个新手,尝试使用工具Selenium学习自动化。我正在尝试自动化这个网站- 在这里,我登录并尝试访问flight finder的单选按钮(单向、双向)。 但是我得到了错误无法定位元素 尝试了以下方法 尝试使用从firebug获取的Xpath定位元素 使用以下由html代码组成的Xpath来定位单选按钮 //*[@type='radio']//*[@value='oneway'] //*[contains(@type,'radio')] //*[contains(text(),'one
Selenium
学习自动化。我正在尝试自动化这个网站-
在这里,我登录并尝试访问flight finder的单选按钮(单向、双向)。
但是我得到了错误无法定位元素
尝试了以下方法
尝试使用从firebug
获取的Xpath
定位元素
使用以下由html
代码组成的Xpath来定位单选按钮
//*[@type='radio']//*[@value='oneway']
//*[contains(@type,'radio')]
//*[contains(text(),'oneway']
//input[@type='radio' AND @value='oneway']
还尝试了CSS选择器
来定位元素
driver.findElement(By.cssSelector("input[type=radio][value=oneway]"))
尝试使用隐式等待
和线程添加等待时间。睡眠
从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和value oneway的元素。。此xpath查找类型为radio的元素,该元素具有值为单向的子元素
/*[包含(@type,'radio')]
-您将获得多个结果
/*[包含(text(),'oneway']
-文本不是单向的,只有value属性是单向的,文本包含'oneway'
//输入[@type='radio'和@value='oneway']
-如果您更改“和”和“到”以及“实际上,问题在于您的测试方法
在TestNG
中,默认情况下,@Test
方法的执行是按字母顺序进行的。因此,在您的代码flightFinder()
方法中,在login()
之前执行,因此即使您使用右定位器单击单选按钮,它也会显示异常
解决方案:
按字母顺序维护方法名称
对方法使用@Test
注释下的优先级,例如-@Test(优先级=1)
创建依赖性测试,例如-
@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");
}
以下解决方案在我们的网站上对我有效-
driver.findElement(By.cssSelector("input[value='oneway']")).click();
此网站需要授权,因此您最好在问题中添加目标单选按钮的HTML
。还要检查它是否位于iframe
内,如果是,请为此iframe
提供HTML
。可能是链接文本有一些填充或其他内容,并且不完全匹配,请尝试按cl使用ass或id。感谢您的输入:)尝试了此xpath“//input[@type='radio'和@value='oneway']”。但它不起作用。我没有得到这样的元素例外。对我来说很好。非常感谢:)@Ashley,请记住测试应该独立于其他测试。正如Narendra所建议的,按字母顺序执行测试只是默认设置,可以更改。