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

Nih*_*vel 3 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>
Run Code Online (Sandbox Code Playgroud)

爪哇:

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();
}
Run Code Online (Sandbox Code Playgroud)

如果我使用,真的没关系:

driver.findElement(By.id("id8-1")).click();
Run Code Online (Sandbox Code Playgroud)

或者

driver.findElementById("id8-1").click();
Run Code Online (Sandbox Code Playgroud)

或者

driver.findElement(By.xpath("//form[1]/input[6]")).click();
Run Code Online (Sandbox Code Playgroud)

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

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

更新1:

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

我很惊讶。昨天明明是八点。不过,我不知道使用 cssSelector 解决方案是否最好,因为我想使用 ID。我赞成所有答案,因为所有答案都很有用,但我希望使用 ID,所以我正在使用我的代码.. 如果您身边有更新,我会选择一个 (: (:

谢谢大家!!!

Big*_*kin 5

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