2012-07-11 29 views
19

Tôi đang sử dụng trình quản trị web Selenium. Tôi không thể chọn tùy chọn (nói 2) từ các Tùy chọn được mở khi nhấp chuột phải.Chọn một tùy chọn từ Menu chuột phải trong Trình quản lý web của Selenium - Java

Trong mã hiện tại của tôi, tôi có thể nhấp chuột phải vào webElement nhưng không thể chọn Tùy chọn từ danh sách được mở sau khi nhấp chuột phải, vì nó tự động biến mất.

Actions action= new Actions(driver); 
action.contextClick(productLink).build().perform(); 

Vì vậy, với mã này tôi có thể nhấp chuột phải nhưng menu nhấp chuột phải tự động biến mất. Tôi muốn chọn nói Tùy chọn thứ 2 từ menu Nhấp chuột phải.

Xin vui lòng trợ giúp !!!

Trả lời

27

Để chọn mục từ menu ngữ cảnh, bạn phải chỉ di chuyển của bạn vị trí chuột với việc sử dụng các sự kiện quan trọng như thế này: -

Actions action= new Actions(driver); 
action.contextClick(productLink).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform(); 

hy vọng điều này sẽ làm việc cho bạn. Chúc một ngày tuyệt vời :)

+0

@ Neeraj- Điều này đang hoạt động tốt .... Cảm ơn rất nhiều !!! –

1

Bạn có thể phải di chuyển chuột đến bất kỳ địa điểm cụ thể sau bối cảnh nhấp chuột() như thế này -

Actions action = new Actions(driver); 
actions.contextClick(link).moveByOffset(x,y).click().build().perform(); 

Để hiểu làm thế nào moveByOffset (x, y) công trình trông here;

Tôi hy vọng công trình này hoạt động. Bạn sẽ phải tính giá trị bù trừ cho xy;

cách tốt nhất là tìm kích thước của mỗi nút tùy chọn sau khi nhấp chuột phải và sau đó nếu bạn nhấp vào tùy chọn thứ 2.

x = chiều rộng của nút tùy chọn/2

y = 2 * (kích thước của mỗi nút tùy chọn)

+0

@ Hari Reddy - Cảm ơn bạn đã trả lời, Trong vấn đề giải pháp hiện tại là tôi không thể o có được các giá trị bù đắp bởi vì "Tùy chọn menu chuột phải" không thể được kiểm tra trong firebug. Vì vậy, không thể để có được offsets trong Firebug> bố trí. –

+0

vâng tôi hiểu vấn đề của bạn. Thậm chí tôi không chắc chắn làm thế nào chúng ta có thể tìm thấy chiều rộng của các tùy chọn chúng tôi nhận được sau khi nhấp chuột phải. Bạn có thể thử một số cú đánh và đường mòn, thử tăng thêm 5 hoặc 10. Cho tôi biết điều gì sẽ xảy ra? –

0

Đây là mã cho Nhấp chuột phải vào một webelement.

Actions actions = new Actions(driver);  
Action action=actions.contextClick(WebElement).build(); //pass WebElement as an argument 
      action.perform(); 
4

đây là cách tiếp cận tốt hơn và thành công của nó:

Actions oAction = new Actions(driver); 
oAction.moveToElement(Webelement); 
oAction.contextClick(Webelement).build().perform(); /* this will perform right click */ 
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */ 

elementOpen.click(); 
+0

có thể hoạt động cho các menu tùy chỉnh nhưng không hoạt động đối với trình đơn trình duyệt Chrome – iirekm

3

Chúng tôi sẽ mất sự giúp đỡ của lớp action WebDriver và thực hiện Right Click. dưới đây là cú pháp:

Actions action = new Actions(driver).contextClick(element); 
action.build().perform(); 

Dưới đây là những bước chúng tôi đã theo trong ví dụ:

  1. Xác định yếu tố
  2. Chờ cho sự hiện diện của phần tử
  3. Bây giờ thực hiện Context nhấp chuột
  4. Sau đó, chúng tôi cần chọn liên kết được yêu cầu.

gói com.pack.click chuột phải;

import org.openqa.selenium.Alert; 
    import org.openqa.selenium.By; 
    import org.openqa.selenium.NoSuchElementException; 
    import org.openqa.selenium.StaleElementReferenceException; 
    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.support.ui.ExpectedConditions; 
    import org.openqa.selenium.support.ui.WebDriverWait; 
    import org.testng.Assert; 
    import org.testng.annotations.AfterClass; 
    import org.testng.annotations.BeforeClass; 
    import org.testng.annotations.Test; 

public class RightClickExample { 

    WebDriver driver; 

    String URL = "http://medialize.github.io/jQuery-contextMenu/demo.html"; 

    @BeforeClass 
    public void Setup() { 
     driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
    } 

    @Test 
    public void rightClickTest() { 
     driver.navigate().to(URL); 
     By locator = By.cssSelector(".context-menu-one.box"); 
     WebDriverWait wait = new WebDriverWait(driver, 5); 
     wait.until(ExpectedConditions.presenceOfElementLocated(locator)); 
     WebElement element=driver.findElement(locator); 
     rightClick(element); 
     WebElement elementEdit =driver.findElement(By.cssSelector(".context-menu-item.icon.icon-edit>span")); 
     elementEdit.click(); 
     Alert alert=driver.switchTo().alert(); 
     String textEdit = alert.getText(); 
     Assert.assertEquals(textEdit, "clicked: edit", "Failed to click on Edit link"); 
    } 

    public void rightClick(WebElement element) { 
     try { 
      Actions action = new Actions(driver).contextClick(element); 
      action.build().perform(); 

      System.out.println("Sucessfully Right clicked on the element"); 
     } catch (StaleElementReferenceException e) { 
      System.out.println("Element is not attached to the page document " 
        + e.getStackTrace()); 
     } catch (NoSuchElementException e) { 
      System.out.println("Element " + element + " was not found in DOM " 
        + e.getStackTrace()); 
     } catch (Exception e) { 
      System.out.println("Element " + element + " was not clickable " 
        + e.getStackTrace()); 
     } 
    } 

    @AfterClass 
    public void tearDown() { 
     driver.quit(); 
    } 


} 
0

Đây là cách tôi có thể nhấp vào phần tử thứ tư trong Right click window.

Actions myAction = new Actions(driver); 
myAction.contextClick(driver.findElement(By.xpath("//ul/li[1]/a/img"))).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ARROW_DOWN).build().perform(); 
myAction.sendKeys(Keys.ENTER).build().perform(); 

Hope this helps

1

* Sử dụng lớp Robot bạn có thể làm được điều này, Hãy thử đoạn mã sau:

Actions action = new Actions(driver); 
action.contextClick(WebElement).build().perform(); 
Robot robot = new Robot(); 
robot.keyPress(KeyEvent.VK_DOWN); 
robot.keyRelease(KeyEvent.VK_DOWN); 
robot.keyPress(KeyEvent.VK_ENTER); 
robot.keyRelease(KeyEvent.VK_ENTER);* 
0

Nhấp chuột phải có thể đạt được sử dụng Java script thi hành di chúc cũng như (trong trường hợp hành động lớp học không được hỗ trợ):

JavascriptExecutor js = (JavascriptExecutor) driver; 

String javaScript = "var evt = document.createEvent('MouseEvents');" 
       + "var RIGHT_CLICK_BUTTON_CODE = 2;" 
       + "evt.initMouseEvent('contextmenu', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, RIGHT_CLICK_BUTTON_CODE, null);" 
       + "arguments[0].dispatchEvent(evt)"; 

js.executeScript(javaScript, element); 
Các vấn đề liên quan