2016-04-27 97 views
1

在一個項目中,我想右鍵點擊一個選項,並從那裏我想選擇「在新窗口中打開鏈接」。我寫了下面的selenium-java代碼。在這段代碼中,它是在「在新窗口中打開鏈接」進行爬網,但之後沒有單擊該選項在新窗口中打開我需要的鏈接。如果你想要,你可以直接複製和粘貼我的代碼來可視化執行流程。請幫我明白的地方是我做了錯誤的....我的目的是在新窗口中打開鏈接從右鍵單擊菜單它不是點擊一個特定的選項Selenium

package pack_001; 

import java.util.Set; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.JavascriptExecutor; 
import org.openqa.selenium.Keys; 

public class mail { 
    public static WebDriver driver=new FirefoxDriver(); 
    public static void main(String args[]) 
    { 
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
      driver.get("http://www.tutorialspoint.com/java/"); 
      driver.manage().window().maximize(); 
      WebElement env=driver.findElement(By.linkText("Java - Environment Setup")); 
      System.out.println("Env point out"); 
      Actions oAction = new Actions(driver); 

      oAction.moveToElement(env); 
      oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).click().build().perform(); /* this should click on the option*/ 



    } 

} 
+0

要獲得同樣的效果,你可以創建一個新的'WebDriver'並導航到該鏈接。例如:'driver2.get(env.getAttribute(「href」));' – Titus

+0

如果您不介意,請您詳細說明這一部分。我的意思是,如果我已經確定了一個名爲'element'的鏈接WebElement,我該如何在新窗口中打開它...... – RCode

+0

要創建一個新窗口,您需要創建一個新的'WebDrive',例如:'WebDriver driver2 = new FirefoxDriver();',並在這個新窗口中打開'element'鏈接,可以這樣做'drive2.get(element.getAttribute(「href」));'。如果'element'是一個HTML''元素,這將起作用。另外,Firefox有一個快捷方式可以在新窗口中打開一個鏈接:SHIFT +左鍵單擊。 – Titus

回答

0

HI,而不是點擊使用ENTER它將工作

oAction.moveToElement(env); 
oAction.contextClick(env).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform(); 

也爲什麼點擊給出webElement和新標籤選項中是行不通的原因點擊點擊是不是webElement

在新windwo這必將打開標籤

UPDATE

public class DropDownSelection { 
    public static WebDriver driver=new FirefoxDriver(); 
    public static void main(String[] args) { 
      driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

     // i have used a sample url exactly as similar to your problem 
     driver.get("http://desirulez.biz/category/wrestlingnetwork/wwe/wwe-raw/"); 
     driver.manage().window().maximize(); 

     // way One 
     Actions oAction = new Actions(driver); 

     // now simply hover over the WWE - Menu in your case 
     WebElement Menu = driver.findElement(By.xpath("//*[@id='menu-item-8']/a")); 
     oAction.moveToElement(Menu).build().perform(); 
     // hovering will open its sub-menu - Configure in your case 
     // now identify the sub-menu that you want to click/hover 
     WebElement Configure = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/a")); 
     oAction.moveToElement(Configure).build().perform(); 
     // now hovering over it will open its sub menu 
     // in your case Manage 
     // now identify the sub-menu that you want to click/hover 
     WebElement Manage = driver.findElement(By.xpath("//*[@id='menu-item-8']/ul/li[7]/ul/li/a")); 
     oAction.moveToElement(Manage).click().build().perform(); 

     // way Two 
     // note if you want to chain above you can even do that like below 
     oAction.moveToElement(Menu).moveToElement(Configure).moveToElement(Manage).click().build().perform(); 
} 
} 

希望這能解決您的查詢

+0

謝謝@raj ....讓我試試,我會讓你知道我的進步.... – RCode

+0

讓我更詳細地解釋你的目標。您的解決方案在大多數網站中都很好,我也發現這是一種方便的方式。但是我實際上試圖在新窗口中打開鏈接的環境並不穩定。有時它正在工作,有時甚至失敗。流程是這樣的 - 它會點擊「菜單」選項 - >下來列表 - >選擇「配置」選項 - >另一個子菜單 - >現在我需要打開「管理」在一個新的窗口。所以有高級的「管理」選項沒有問題。但上述解決方案有時點擊「管理」有時成功 – RCode

+0

有時它不能點擊它...你能幫我嗎... – RCode