无法循环子URL并且无法在新窗口中打开 - selenium java

时间:2017-06-11 18:09:23

标签: java selenium dom

这是我在Java中使用Selenium WebDriver的第一个任务,我在HTML下拉列表中遇到以下问题。

<select name="categorySearch" id="categorySearch" onchange="GetURL(this, this.selectedIndex);"><option selected="selected" value="">Choose Category</option><option url="document_category.asp?cat_id=8&amp;name=2006+Approved+Budget" value="8">2006 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=9&amp;name=2006+Approved+Special+Assessment" value="9">2006 Approved Special Asses (1)</option>
<option url="document_category.asp?cat_id=11&amp;name=2007+Approved+Budget" value="11">2007 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=12&amp;name=2007+Maintenance+Schedule" value="12">2007 Maintenance Schedule (1)</option>
<option url="document_category.asp?cat_id=14&amp;name=2008+Approved+Budget" value="14">2008 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=19&amp;name=2008+Audited+Financials" value="19">2008 Audited Financials (1)</option>
<option url="document_category.asp?cat_id=15&amp;name=2008+Maintenance+Schedule" value="15">2008 Maintenance Schedule (1)</option>
<option url="document_category.asp?cat_id=16&amp;name=2008+Special+Assessment" value="16">2008 Special Assessment (1)</option>
<option url="document_category.asp?cat_id=20&amp;name=2009+Approved+Budget" value="20">2009 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=23&amp;name=2010+Audited+Financials" value="23">2010 Audited Financials (1)</option>
<option url="document_category.asp?cat_id=22&amp;name=2011+Approved+Budget" value="22">2011 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=26&amp;name=2011+Audited+Financials" value="26">2011 Audited Financials (1)</option>
<option url="document_category.asp?cat_id=24&amp;name=2012+Approved+Budget" value="24">2012 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=28&amp;name=2012+Audited+Financial+Reports" value="28">2012 Audited Financial Repo (0)</option>
<option url="document_category.asp?cat_id=25&amp;name=2012+Maintenance+Fees" value="25">2012 Maintenance Fees (1)</option>
<option url="document_category.asp?cat_id=29&amp;name=2013+Audited+Financial+Report" value="29">2013 Audited Financial Repo (1)</option>
<option url="document_category.asp?cat_id=27&amp;name=2013%2D2014+Approved+Budget" value="27">2013-2014 Approved Budget (1)</option>
<option url="document_category.asp?cat_id=32&amp;name=2014+5%2DYear+Capital+Improvement+Plan" value="32">2014 5-Year Capital Improve (0)</option>
<option url="document_category.asp?cat_id=33&amp;name=2014+Audited+Financials" value="33">2014 Audited Financials (1)</option>
<option url="document_category.asp?cat_id=30&amp;name=2015+Approved+Budget" value="30">2015 Approved Budget (2)</option>
<option url="document_category.asp?cat_id=31&amp;name=2015+Reserve+Schedule" value="31">2015 Reserve Schedule (0)</option>
<option url="document_category.asp?cat_id=18&amp;name=Amendment+2008" value="18">Amendment 2008 (1)</option>
<option url="document_category.asp?cat_id=21&amp;name=Amendment+2009" value="21">Amendment 2009 (1)</option>
<option url="document_category.asp?cat_id=34&amp;name=Amendment+2016" value="34">Amendment 2016 (1)</option>
<option url="document_category.asp?cat_id=10&amp;name=Board+of+Director+Review" value="10">Board of Director Review (1)</option>
<option url="document_category.asp?cat_id=1&amp;name=Declaration" value="1">Declaration (1)</option>
<option url="document_category.asp?cat_id=5&amp;name=Forms%2FApplications" value="5">Forms/Applications (11)</option>
<option url="document_category.asp?cat_id=17&amp;name=Limited+Proxy%2D+Annual+Meeting" value="17">Limited Proxy- Annual Meeti (2)</option>
<option url="document_category.asp?cat_id=3&amp;name=Maps" value="3">Maps (1)</option>
<option url="document_category.asp?cat_id=6&amp;name=Meeting+Minutes" value="6">Meeting Minutes (113)</option>
<option url="document_category.asp?cat_id=7&amp;name=Procedures+for+Selling+Units" value="7">Procedures for Selling Unit (1)</option>
<option url="document_category.asp?cat_id=2&amp;name=Rules+and+Regulations" value="2">Rules and Regulations (2)</option>
<option url="document_category.asp?cat_id=13&amp;name=Wrtten+Request+for+Candidacy" value="13">Wrtten Request for Candidac (1)</option>
</select>

我有以下代码来提取网址

    public void getList()throws InterruptedException{
        WebElement list = driver.findElement(By.xpath("//select[@name='categorySearch']"));

        List<WebElement> options = list.findElements(By.tagName("option"));
        for(WebElement option : options){               listLinks.add("http://myminorca.com/"+option.getAttribute("url"));                  
        }
        for(String url : listLinks){
            System.out.println(url);
        }

所有listLinks页面中都有0,1,2,11和113个链接。表单/应用程序中的11个链接示例。

<div class="ahn-list-items">
<div class="ahn-standard-listing" data-featureid="8" data-itemid="5">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=5" class="ahn-listing-title ahn-hover-link" target="main">Architectural Contol Committee Application Form</a><br>2 views (30 KB) <a href="document_view.asp?id=5" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="7">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=7" class="ahn-listing-title ahn-hover-link" target="main">Complaint Form</a><br>1 views (26 KB) <a href="document_view.asp?id=7" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="8">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=8" class="ahn-listing-title ahn-hover-link" target="main">Incident Form</a><br>1 views (29 KB) <a href="document_view.asp?id=8" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="9">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=9" class="ahn-listing-title ahn-hover-link" target="main">Pet Registration</a><br>1 views (30 KB) <a href="document_view.asp?id=9" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="10">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=10" class="ahn-listing-title ahn-hover-link" target="main">Recreation Room</a><br>1 views (28 KB) <a href="document_view.asp?id=10" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="21">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=21" class="ahn-listing-title ahn-hover-link" target="main">Agenda Item Registration Form</a><br>1 views (24 KB) <a href="document_view.asp?id=21" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="22">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=22" class="ahn-listing-title ahn-hover-link" target="main">Application for Occupancy</a><br>8 views (30 KB) <a href="document_view.asp?id=22" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="25">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=25" class="ahn-listing-title ahn-hover-link" target="main">Parking Permit Registration</a><br>1 views (30 KB) <a href="document_view.asp?id=25" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="28">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=28" class="ahn-listing-title ahn-hover-link" target="main">Certificate of Appointment (Voting Rep.)</a><br>1 views (29 KB) <a href="document_view.asp?id=28" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="29">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=29" class="ahn-listing-title ahn-hover-link" target="main">Candidacy Request Form</a><br>1 views (25 KB) <a href="document_view.asp?id=29" target="main">[more]</a></p></div><div class="clear"></div></div>
<div class="ahn-standard-listing" data-featureid="8" data-itemid="33">
<div class="ahn-favorite-icon"></div>
<div class="clear"></div><div class="ahn-listing-description"><div class="ahn-listing-date"></div><p><a href="document_view.asp?id=33" class="ahn-listing-title ahn-hover-link" target="main">Limited Proxy- Special Meeting 6/27/06</a><br>1 views (34 KB) <a href="document_view.asp?id=33" target="main">[more]</a></p></div><div class="clear"></div></div>
</div>

我有以下代码来遍历上述URL并尝试在新窗口中打开。上面的URL包括word文件,xls或pdf。 PDF链接是我想在新窗口中打开的原因,因为它们是在点击时打开而不是直接下载,其次是他们使用StaleElementreferenceexception制作我的DOM Stale。 评论的部分是我尝试过但失败的。

    public List<String> getDocs(String link) throws InterruptedException{
    //  int count = 0;
        List<String> linkDocs = new ArrayList<String>();

        driver.navigate().to(link);
             try{
             List<WebElement> documentLinks = driver.findElements(By.xpath("//div[@class='ahn-listing-description']"));

             for(WebElement documentLink : documentLinks){
                 driver.navigate().to(link);
    //           count++;
    //           System.out.println(link+" "+count+" "+documentLink.getText());
                 try{
                 docLinks.add(documentLink.findElement(By.tagName("a")).getAttribute("href"));
                 linkDocs.add(documentLink.findElement(By.tagName("a")).getAttribute("href"));

//               String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
//                  driver.findElement(By.linkText(documentLink.getText())).sendKeys(selectLinkOpeninNewTab);
//               String newWindow = Keys.chord("w");
                 documentLink.click();  
                 Thread.sleep(15000);




//               cont.close();
//               WebElement elementOpen = driver.findElement(By.linkText("Open Link in New Window"));
//               elementOpen.click();
//               newwin.keyDown(Keys.SHIFT).click(documentLink).keyUp(Keys.SHIFT).build().perform();
//               newwin.moveToElement(documentLink.findElement(By.tagName("a"))).contextClick().sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).build().perform();
//               newwin.moveToElement(documentLink.findElement(By.tagName("a"))).contextClick().sendKeys("w");
//                  String newTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
//                  documentLink.findElement(By.tagName("a")).sendKeys(newTab);
//               System.out.println(docLinks);
                 }
                 catch (NoSuchElementException e){
                        System.out.println(documentLink.getText()+" has no link");
                    }
             }
             }
             catch(StaleElementReferenceException e){
                 return linkDocs;
             }
             return linkDocs;

    }

先谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试下面提到的步骤来解决您的问题。我还添加了一个代码段,您可以通过该代码段在新选项卡中打开链接。

<强>步骤:

  1. 首先在列表中存储您想要的任何链接。

    <强> linkDocs.add(documentLink.findElement(By.tagName(&#34;&#34;。))的getAttribute(&#34; HREF&#34));

  2. 遍历列表:

    for(String link:linkDocs)

    {
        openLinkInNewTab(link);
    }
    
  3. 以下是将在新标签页中打开链接的java代码

    public static void openLinkInNewTab(link){
    
     // Open a new tab
     JavascriptExecutor js = (JavascriptExecutor) driver;  
     js.executeScript("window.open();");
    
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
    
     // Switch to the child tab
      driver.switchTo().window(tabs.get(1));
      driver.get("link which you want to open ");
    
    //Add whatever validations you want.
    
      // Switch to the parent tab.
      driver.switchTo().window(tabs.get(0));
    

    }