How to click in a specific part of a Button using

2020-03-05 06:05发布

问题:

<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>

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!

回答1:

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.

Hope this helps you.



回答2:

Using the following command _click(_xy(_cell("New"),-5,5)); in sahi solved my problem...!!!