如何使用Selenium单击按钮的特定部分以显示选项列表? [英] How to click in a specific part of a Button using Selenium, for the list of options to be displayed?

查看:85
本文介绍了如何使用Selenium单击按钮的特定部分以显示选项列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<table id="ext-comp-1389" class="x-btn x-btn-text-icon " cellspacing="0" style="width: auto;">
<tbody class="x-btn-small x-btn-icon-small-left">
<tr>
<tr>
<td class="x-btn-ml">
<td class="x-btn-mc">
<em class="x-btn-split" unselectable="on">
<button id="ext-gen128" class="x-btn-text create" type="button">New</button>
</em>
</td>
<td class="x-btn-mr">
<i>&nbsp;</i>
</td>
</tr>
<tr>
</tbody>
</table>

以上是如何嵌入HTML元素..! HTML元素是一个新建"按钮,其旁边带有"+"号...如果仅单击"+",则可以获得菜单选项,如"D","P" ','T'和'U'.如果我点击新建"按钮(在代码中,这部分是 新的 ,单击操作不会显示任何内容,该操作发生在中心位置...下面是按钮的图像...

Above is how the HTML element is embedded..! The HTML element is a 'New' button with a '+' sign next to it...If, I click on the '+' only, I can get the menu options, which are something like, 'D', 'P', 'T' and 'U'. If I click on the 'New' button ( In the code, it is this part, New , nothing gets displayed as the click action, takes place on the center... Below is the image of the button...

这是我尝试过的代码...在过去的几个小时中,

Here is the code, which I have tried...For the last couple of hours,

    package com.hr.testpack;
import static org.junit.Assert.fail;
    import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
    import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
    import com.thoughtworks.selenium.*;

public class Classtest2 {

static Selenium selenium;
static final String HOST = "localhost";
static final int PORT = 4444;
static final String BROWSER = "*firefox";
static final String URL = "http://test.test.com";
private static int buttonwidth=42;//value got from firebug computation tab...
    private static final int Xoffset = (buttonwidth/2)+6;
private static final int Yoffset = 0;
private WebDriver driver;
private String baseUrl;

private StringBuffer verificationErrors = new StringBuffer();
//private StringBuffer verificationErrors = new StringBuffer();

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://test.test.com/RMprojectProject";
    driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
}


@Test

public void testrr1() throws Exception
{


/*  Only Driver...Driver's way to open a URL*/

driver.get(baseUrl + "/");  


/* Webdriver Backed Selenium method- 

//*selenium = new WebDriverBackedSelenium(driver, baseUrl);

//*selenium.open("/");

//selenium.wait(15000); - Never use Wait, when Implicit wait is used


//WebDriver driver = ((WebDriverBackedSelenium) selenium).getWrappedDriver();

//Driver should be instantiated for using Driver's methods for

// SeleniumBackedWebdriver

     // Comments over
     */


driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("test@test.com");

driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("test124");


/* For normal Selenium RC and SeleniumBackedWebDriver method

selenium.type("username", "test");
selenium.type("password","test124");

*/

/* Internal Wait methods, which can be used for normal web
 * 
 * applications. Methods written in the end of the program
 * 
 * waitFor("xpath=.//*[@id='loginButton']/tbody/tr[2]/td[2]'");
 waitSecond(20); */


driver.findElement(By.xpath(".//*[@id='loginButton']/tbody/tr[2]/td[2]")).click();

//selenium.waitForPageToLoad("85000");

//selenium.waitForCondition("selenium.isElementPresent(\"xpath=.//*[@id='ext-gen165']/div/table/thead/tr/td[3]\")", "70000");



WebElement ele = driver.findElement(By.xpath("//*[@id='ext-gen128']"));
Actions build = new Actions(driver);

build.moveToElement(ele, Xoffset, Yoffset).click().build().perform();




    //  selenium.waitForCondition("selenium.isElementPresent(\"xpath=//*[@id='ext-gen246']\")", "20000");

driver.findElement(By.xpath("//*[@id='ext-gen245']")).click();

  }



@After

public void tearDown() throws Exception{

    selenium.stop();
    //driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
        fail(verificationErrorString);
    }
}

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

/* The Below methods can be used, for page loading/waits in 
 * 
 * normal web applications...waitFor(seconds),when called in
 * 
 * the application, the wait happens...useful in occasions

public void waitFor(String locator) throws Exception {
    for (int second = 0;; second++) {
        if (second >= 60)
            fail("timeout");
        try {
            if (selenium.isVisible(locator))
                break;
        } catch (Exception e) {
        }
        Thread.sleep(1000);
    }
}

public void waitSecond(int n) throws Exception {
    for (int second = 0;; second++) {
        if (second >= 60)
            fail("timeout");
        try {
            if (second > n - 1)
                break;
        } catch (Exception e) {
        }
        Thread.sleep(1000);
    }
}

*/

代码已成功执行,并且测试用例通过了,但是当单击"+"按钮时,应该看到的元素却看不见!

Code is getting executed successfully and test case is pass, but the element which should be seen, when clicked on the '+' button are not seen!

推荐答案

通过以下方式使用操作-

Use Actions in the following way -

    WebElement ele = driver.findElement(By.xpath("//*[@id='ext-gen128']");
Actions build = new Actions(driver);
build.moveToElement(ele, Xoffset, Yoffset).click().build().perform();

可能通过使用偏移量,您可以准确地使硒驱动器单击您提到的+按钮.

Probably by using offset you can exactly make the selenium driver click on the + button you have mentioned.

希望这对您有所帮助.

这篇关于如何使用Selenium单击按钮的特定部分以显示选项列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆