java - Selenium 无法通过 id 和 xpath 找到元素

标签 java selenium

我已经阅读了关于这个主题的所有其他问题,有很多。我尝试了一些,但我没有在代码中发现错误。

我也尝试添加一个计时器来等待页面加载。

在html代码和java下面:

HTML:

<form id="myform" method="get" action="">
    <input type="hidden" name="something1" id="something1.1" />
    <input type="hidden" name="something2" value="" />
	<table>
        <tr>
            <td><label>Name: </label></td>
            <td><select name="name">
                <option selected="selected" value="1000">FirstNameOnly</option>
            </select></td>
        </tr>
        <tr>
            <td><label>Direction: </label></td>
            <td><select name="Direction">
                <option selected="selected" value="">Choose One</option>
                <option value="UP">UP</option>
            </select></td>
        </tr>
        <tr>
            <td colspan="2"><label>Time: </label></td>
        </tr>
        <tr>
            <td><label>From: </label></td>
            <td><input type="text" value="" name="from" id="id6"/>
        </tr>
        <tr>
            <td><label>To: </label></td>
            <td><input type="text" value="" name="to" id="id7"/>
        </tr>
        <tr>
            <td><label>File type: </label></td>
            <td><span id="id8">
            <input name="fileType" type="radio" checked="checked" value="0" id="id8-0"/><label for="id8-0">Excel</label>
            <input name="fileType" type="radio" value="1" id="id8-1"/><label for="id8-1">CSV</label>
            </span></td>
        </tr>
        <tr>
        <td></td>
        <td>
            <input type="submit" name="p::submit" id="id9" value="Preview">
            <input type="submit" name="download" id="ida" value="Download">
        </td>
        </tr>
    </table>
</form>

Java:

public void HeadlessChromeStartDownload(){

    System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    if (ValidateOS.isWindows()){
        options.setBinary("C:\\Users\\Juri\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe");
        System.out.println("Windows system");
    } else if (ValidateOS.isUnix()){
        options.setBinary("/path/to/chrome/not/yet/added");
    }

    options.addArguments("--headless --disable-gpu");
    ChromeDriver driver = new ChromeDriver(options);

    driver.get("http://localhost/that-test-page.html");
    //WebElement timer = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("id8-1")));

    WebElement select1 = driver.findElementByName("FirstNameOnly");
    Select field1 = new Select(select1);
    field1.selectByIndex(1);

    WebElement select2 = driver.findElementByName("Direction");
    Select field2 = new Select(select2);
    field2.selectByIndex(1);

    driver.findElementByName("from").sendKeys("21/06/2017");

    driver.findElementByName("to").sendKeys("22/06/2017");

    /*File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    try {
        FileUtils.copyFile(scrFile, new File("C:\\screen1.jpg"));
        System.out.println("Screen saved");
    } catch (IOException e) { System.out.println("Screen NOT saved"); }
    */

    //driver.findElement(By.id("id8-1")).click();
    driver.findElement(By.xpath("//form[1]/input[6]")).click();

    //driver.findElementById("ida").click();

    driver.quit();
}

如果我使用真的没关系:

driver.findElement(By.id("id8-1")).click();

driver.findElementById("id8-1").click();

driver.findElement(By.xpath("//form[1]/input[6]")).click();

我无法让 Selenium 单击该单选按钮。 其余代码也是如此,事实上我使用了 findElementByName,这显然不是最佳选择。

感谢任何知道这段代码有什么问题的人!! (: (:

更新1:

所以,我无法解释昨天发生的事情。我正在尝试测试的网站使用 id8-1 作为该单选按钮。今天是id3-1,两个解决方案都是:driver.findElement(By.cssSelector("input[value='1']")).click(); 或我的:driver.findElement(By.id("id3-1")).click(); 有效。

我很惊讶。昨天显然是8点。 不过,我不知道使用 cssSelector 解决方案是否最好,因为我想使用 ID。 我对所有答案都投了赞成票,因为它们都很有用,但我希望使用 ID,所以我正在使用我的代码。如果您需要更新,我会选择 (: (:

感谢大家!!!

最佳答案

用Selenium 驱动IE 时,遇到一个问题driver.find_element_by_id("elem_id")找不到它但正在运行 javascript document.getElementById("elem_id")在IE的开发者控制台中即可。就我而言,我正在提取一个值。所以我通过做 driver.execute_script('return document.getElementById("elem_id").outerText') 解决了这个问题

关于java - Selenium 无法通过 id 和 xpath 找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44703065/

相关文章:

java - 如果请求参数超过 15 个,POST 或 GET 应该使用什么?

java - SQL语句中的错误

java.text.ParseException : Unparseable date on some devices only 异常

java - 没有找到给定的测试包括 : [***Test](filter. includeTestsMatching)

ruby - 使用 Selenium 和 Ruby 自动化 Chrome 扩展

java - AWS Lambda 的 Chrome 二进制文件

java - 运行Map精简程序时的大数据和hadoop异常

android - 从 adb logcat 捕获输出

c# - Selenium 查找指定元素内的元素

java - TestNG 中的非静态 Webdriver 驱动程序和屏幕截图监听器