Selenium按钮单击不会导致下一页

时间:2018-01-14 14:45:36

标签: java macos selenium selenium-chromedriver

我一直在研究这个问题。我查看了多个StackOverflow帖子以及场外帖子。

我的问题似乎有所不同,因为点击或提交没有错误,按钮可见,但点击并不会导致预期的页面加载所需的结果。

正如您在下面评论部分所看到的,我尝试了几种方法来获得点击/提交的结果。

感谢任何帮助

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
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.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class HeadlessChrome {

    // private static final String STREET_NUMBER = "ctl00_pageContent_ctl00_txtStreetNumber";
    // private static final String STREET_NAME = "ctl00$pageContent$ctl00$txtStreet";
    // private static final String CITY = "ctl00$pageContent$ctl00$txtCity";
    // public static final String SEARCH_BUTTON = "ctl00$pageContent$ctl00$btnSearch";
    //
    // public static final String RESULTS_PANEL = "ctl00_pageContent_ctl00_pnlResults";

    @Test
    public void createChromeDriverHeadless() throws IOException {
        System.setProperty("webdriver.chrome.driver", "/Volumes/Lagoon/Users/heiden/Documents/Java/chromedriver");

        final ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setBinary("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
        chromeOptions.addArguments("--headless");

        final WebDriver driver = new ChromeDriver(chromeOptions);

        driver.navigate().to("http://www.dupageco.org/PropertyInfo/PropertyLookup.aspx");

        final WebDriverWait waitForWebsite = new WebDriverWait(driver, 5);
        waitForWebsite.until(ExpectedConditions.visibilityOfElementLocated(By
                .name("ctl00$pageContent$ctl00$txtStreetNumber")));

        final Actions actions = new Actions(driver);

        actions.moveToElement(driver.findElement(By.name("ctl00$pageContent$ctl00$txtStreetNumber")));
        actions.click();
        actions.sendKeys("1202");

        actions.moveToElement(driver.findElement(By.name("ctl00$pageContent$ctl00$txtStreet")));
        actions.click();
        actions.sendKeys("Willow");

        actions.moveToElement(driver.findElement(By.name("ctl00$pageContent$ctl00$txtCity")));
        actions.click();
        actions.sendKeys("Wheaton");


        final WebElement searchButton = driver.findElement(By.name("ctl00$pageContent$ctl00$btnSearch"));
        // actions.moveToElement(searchButton);
        // actions.click();

        actions.build().perform();

        // final JavascriptExecutor jse = (JavascriptExecutor) driver;
        // jse.executeScript("window.scrollTo(0," + searchButton.getLocation().y + ")");

        searchButton.submit();

        final WebDriverWait waitForResponse = new WebDriverWait(driver, 5);
        waitForResponse.until(ExpectedConditions.visibilityOfElementLocated(By
                .name("ctl00_pageContent_ctl00_pnlResults")));
        //
        // driver.findElement(By.name("ctl00_pageContent_ctl00_pnlResults"));

        final String expectedURL = "http://www.dupageco.org/PropertyInformation.aspx";
        Assert.assertEquals(expectedURL, driver.getCurrentUrl());

        driver.quit();
    }
}
BTW,静态类变量没有被直接使用,因为By.name在我的测试中不会完成,除非我直接在方法参数中使用了字符串。这是我见过的最奇怪的事情。

由于

更新(HTML的一部分,希望是有用的部分):

// --></script><div id="Mai

n" name="Main" title="Main" onblur="design_validate_re(/\S+/,this,'Cannot be blank');" xmlns:cms="urn:Ektron.Cms.Controls"><h1>Property Lookup</h1><p>This portal leads to the following information for a parcel:</p><ul><li>Tax bill information plus the ability to <strong>pay current year tax bill </strong>and print a duplicate tax bill.</li><li>Tax distribution information - where do your tax dollars go?  Includes links to the taxing bodies.</li><li>Assessment information, including any reviews or corrections made to assessments.  PLEASE NOTE:  The Township Assessors publish property characteristics and assessment data on          
        <a title="their websites" href="/SOA/1488/">the Assessor websites</a>.</li></ul><p><strong>Parcels can be searched using either parcel number (PIN = Property Index Number) or address.</strong></p><p><span class="warning"></span><a title="View the tax payment calendar" href="/Treasurer/5943/">View the tax payment calendar</a>.     </p></div>


            <span id="ctl00_pageContent_ctl00_errMsg" CssStyle="warning"></span>
<div id="ctl00_pageContent_ctl00_pnlSearch" onkeypress="javascript:return WebForm_FireDefaultButton(event, &#39;ctl00_pageContent_ctl00_btnSearch&#39;)">

    <div style="border:solid 1px black;padding-left:10px;margin:5px">
        <h2>Search by PIN</h2>
        <label for="ctl00_pageContent_ctl00_txtParcel" id="ctl00_pageContent_ctl00_lblParcel">PIN: </label>
        <input name="ctl00$pageContent$ctl00$txtParcel" type="text" maxlength="10" size="10" id="ctl00_pageContent_ctl00_txtParcel" style="width:70px;" /><br />
        <span class="warning">Leave off hyphens.</span>
    </div><span class="larger"></span>
    <div style="text-align:center; color:#29547E"><strong>&mdash; OR &mdash;</strong></div>
    <div style="border:solid 1px black;padding-left:10px;margin:5px">
        <h2>Search by Address</h2>
         <p><strong>To return more results, enter partial street numbers, street names, or cities.  Optional fields can also be left blank.</strong>
        <br />Examples:  '10' instead of '1001', 'Roo' instead of 'Roosevelt', 'Glen' instead of 'Glendale Hts' or 'Glen Ellyn'.</p><br />
        <ul class="PropLookupul">
            <li>
                <label for="ctl00_pageContent_ctl00_txtStreetNumber" id="ctl00_pageContent_ctl00_lblStreetNumber" class="PropLookuplbl">Street Number: </label>
                <input name="ctl00$pageContent$ctl00$txtStreetNumber" type="text" maxlength="6" size="6" id="ctl00_pageContent_ctl00_txtStreetNumber" style="width:70px;" /><strong> * Required</strong>
            </li>
            <li>
                <label for="ctl00_pageContent_ctl00_ddlDir" id="ctl00_pageContent_ctl00_lblDir" class="PropLookuplbl">Direction: </label>
                <select name="ctl00$pageContent$ctl00$ddlDir" id="ctl00_pageContent_ctl00_ddlDir">
        <option selected="selected" value=""></option>
        <option value="N">N</option>
        <option value="S">S</option>
        <option value="E">E</option>
        <option value="W">W</option>

    </select> Optional
            </li>
            <li>
                <label for="ctl00_pageContent_ctl00_txtStreet" id="ctl00_pageContent_ctl00_lblStreet" class="PropLookuplbl">Street: </label>
                <input name="ctl00$pageContent$ctl00$txtStreet" type="text" maxlength="22" size="22" id="ctl00_pageContent_ctl00_txtStreet" style="width:150px;" /><strong> * Required</strong><br />
                <span class="warning">Leave off street designations like 'st', 'street', etc.</span>
            </li>
            <li>
                <label for="ctl00_pageContent_ctl00_txtNumber" id="ctl00_pageContent_ctl00_lblNumber" class="PropLookuplbl">Unit: </label>
                <input name="ctl00$pageContent$ctl00$txtNumber" type="text" maxlength="14" size="14" id="ctl00_pageContent_ctl00_txtNumber" style="width:150px;" /> Optional
            </li>
            <li>
                <label for="ctl00_pageContent_ctl00_txtCity" id="ctl00_pageContent_ctl00_lblCity" class="PropLookuplbl">City: </label>
                <input name="ctl00$pageContent$ctl00$txtCity" type="text" maxlength="30" size="30" id="ctl00_pageContent_ctl00_txtCity" style="width:150px;" /> Optional
            </li>
        </ul>
    </div>


</div>


<div style="text-align:center;">
    <input type="submit" name="ctl00$pageContent$ctl00$btnSearch" value="Search" id="ctl00_pageContent_ctl00_btnSearch" class="btn" />
    <input class="btn" type="submit" value=" Clear " onclick="window.location='/PropertyInfo/PropertyLookUp.aspx';return false;" />
</div>



<div id="ctl00_pageContent_ctl00_ContentFooter">
    <span style="display:none" xmlns:cms="urn:Ektron.Cms.Controls"> </span><script language="JavaScript" type="text/javascript" defer="defer" xmlns:cms="urn:Ektron.Cms.Controls"><!--
function ektLocalizeDate(date, id) {
    setTimeout(function() {
    if (document.getElementById && 10 == date.length) {
        var oTempDate = new Date(date.substr(0,4), parseInt(date.substr(5,2),10)-1, date.substr(8,2));
        document.getElementById(id).innerHTML=(oTempDate.toLocaleDateString ? oTempDate.toLocaleDateString() : oTempDate.toLocaleString());
    }
    }, 1); 
}

2 个答案:

答案 0 :(得分:0)

可能的原因是:

  1. 有几个元素与您的xpath匹配。因此,当您单击时,您只需单击第一个没有重定向到下一页的元素。
  2. 您使用了错误的元素,因为重定向链接或onClick事件可以存在于另一个元素上。所以玩最近的元素。阅读本文:https://www.w3schools.com/jsref/event_onclick.asp
  3. 如果没有任何帮助,请试试这个:
  4. ((JavascriptExecutor)driver).executeScript("arguments[0].click;", button);

答案 1 :(得分:0)

以下代码在firefox中运行正常

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.name("ctl00$pageContent$ctl00$txtStreetNumber")).sendKeys("1202");
        driver.findElement(By.name("ctl00$pageContent$ctl00$txtStreet")).sendKeys("Willow");
        driver.findElement(By.name("ctl00$pageContent$ctl00$txtCity")).sendKeys("Wheaton");
        driver.findElement(By.name("ctl00$pageContent$ctl00$btnSearch")).click();