Java 计数单选按钮不使用多个标记名

Java 计数单选按钮不使用多个标记名,java,selenium,Java,Selenium,File File=新文件(“D:\Selenium 2.48\IEDriverServer.exe”) System.setProperty(“webdriver.ie.driver”,file.getAbsolutePath()); WebDriver驱动程序=新的InternetExplorerDriver(); 驱动程序。获取(“http://www.yatra.com/"); driver.manage().window().maximize(); 列表收音机=driver.findE

File File=新文件(“D:\Selenium 2.48\IEDriverServer.exe”)

System.setProperty(“webdriver.ie.driver”,file.getAbsolutePath());
WebDriver驱动程序=新的InternetExplorerDriver();
驱动程序。获取(“http://www.yatra.com/");
driver.manage().window().maximize();
列表收音机=driver.findElements(按.tagName(“a”));
System.out.println(radio.size());
这是新代码,我现在可以使用xpath了。谢谢。但问题是出现了一个白色页面,结果为零。原始页面需要一些时间。

我想数一数单选按钮的数目。 但我没能做到。我试着通过标记名找到它,结果是零。 HTML对我的理解有点复杂,因为在单选按钮中,没有输入。因此我无法通过
input[@type=]
等找到HTML


请在这方面帮助我。

我认为您需要等待事情解决并通过以下方式加载页面:

在这里,我们正在等待trip类型的容器变得可见,我认为这是一个很好的指示,至少页面的相关部分已加载

然后,您可以计算链接数:

List<WebElement> radio = driver.findElements(By.tagName("a")); 
System.out.println(radio.size());
但是,如果您只想检查有多少“trip type”链接,只需获取“trip type”容器中的所有
a
元素:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement tripType = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.trip-type")));

List<WebElement> radio = tripType.findElements(By.tagName("a")); 
System.out.println(radio.size());
WebDriverWait wait=newwebdriverwait(webDriver,10);
WebElement tripType=wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(“div.trip-type”));
列表收音机=tripType.findElements(按.tagName(“a”));
System.out.println(radio.size());

您应该找到这些比率的正确指示器

建议您使用此xpath

//a[@data-flighttrip]
这是我的剧本

import java.util.List;

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

public class TestSelenium {
    static WebDriver driver;

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "res/chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        driver = new ChromeDriver(capabilities);

        driver.get("http://www.yatra.com/");

        List<WebElement> radio = driver.findElements(By.xpath("//a[@data-flighttrip]"));
        System.out.println(radio.size());

        driver.quit();
    }
}
和司机一起试过了

import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestSelenium {
    static WebDriver driver;

    public static void main(String[] args) throws Exception {
        File file = new File("D:\\IEDriverServer.exe");

        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
        cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

        WebDriver driver=new InternetExplorerDriver(cap);

        driver.get("http://www.yatra.com/");
        driver.manage().window().maximize();

        List<WebElement> radio = driver.findElements(By.xpath("//a[@data-flighttrip]"));
        System.out.println(radio.size());

        driver.quit();
    }
}

html中广播框的输入类型可能是您可以使用的。问题在于IE仅适用于chrome。在chrome中它可以工作。感谢您的帮助。您的代码部分是为我编写的。现在我可以通过xpath识别元素。现在。但当我尝试使用IE时,结果是零,因为第一页是空白的,然后它会转到给定的uRL。我刚刚尝试使用IEDRiver,它工作正常。我正在将IE11与IEDriverServer_x64_2.52.0一起使用,很高兴它能帮助您您好,是的,我想问题在于等待,因为我先得到白色页面,然后在控制台中打印0,甚至在原始页面laods之前。所以我尝试了你的代码,但出现了新的异常。
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement tripType = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.trip-type")));

List<WebElement> radio = tripType.findElements(By.tagName("a")); 
System.out.println(radio.size());
//a[@data-flighttrip]
import java.util.List;

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

public class TestSelenium {
    static WebDriver driver;

    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "res/chromedriver.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();

        driver = new ChromeDriver(capabilities);

        driver.get("http://www.yatra.com/");

        List<WebElement> radio = driver.findElements(By.xpath("//a[@data-flighttrip]"));
        System.out.println(radio.size());

        driver.quit();
    }
}
Starting ChromeDriver (v2.9.248315) on port 14427
3
import java.io.File;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestSelenium {
    static WebDriver driver;

    public static void main(String[] args) throws Exception {
        File file = new File("D:\\IEDriverServer.exe");

        System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
        DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
        cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

        WebDriver driver=new InternetExplorerDriver(cap);

        driver.get("http://www.yatra.com/");
        driver.manage().window().maximize();

        List<WebElement> radio = driver.findElements(By.xpath("//a[@data-flighttrip]"));
        System.out.println(radio.size());

        driver.quit();
    }
}
Started InternetExplorerDriver server (64-bit)
2.52.0.0
Listening on port 6267
Only local connections are allowed
3