1

我试图使用 selenium 网络驱动程序单击我的移动网络应用程序上的按钮。按钮被定位,按钮上的文本可以被派生,甚至点击事件表现良好。但导航不会发生。

我尝试使用 Click() 方法、sendKeys() 方法以及脚本执行器。但无法进一步处理。

代码:

public class TestWeb
{  

    WebDriver driver; 

    private Selenium selenium;   

    @Before
    public void setUp() throws Exception {
      driver = new IPhoneDriver();
      driver.get("http://10.5.95.25/mobilebanking");       
    }

    @Test
    public void TC() throws Exception  { 
        System.out.println("page 1");
        Thread.sleep(5000);
        WebElement editbtn1 = driver.findElement(By.id("ext-comp-1018"));
        String s1 = editbtn1.getText();
        System.out.println(s1);
        editbtn1.click();
        editbtn1.sendKeys(Keys.ENTER);
        ((JavascriptExecutor)driver).executeScript("arguments[0].click;", editbtn1); 

        System.out.println("ok");
    }

@After
    public void tearDown() throws Exception {
         System.out.println("*******Execution Over***********");
    }
}

我分别尝试了 click、sendKeys 和 ScriptExecutor,也尝试了组合。它正在执行而没有任何错误,但导航不会发生。

有没有人可以帮助我用其他方法在按钮上执行点击功能?


内存

4

1 回答 1

1

这可能不是你的问题,但我注意到“ext-comp-”并且猜你正在使用 extjs。

我正在使用 GXT,虽然通过 id 查找对很多事情都有效,但在某些提交按钮上却没有。

我不得不在 firefox 中使用 firebug 来定位元素并复制 xpath。然后我可以点击元素

driver.findElement(By.xpath("//div[@id='LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();  // worked

对我来说,它也在默默地失败。我的提交按钮的 ID 为 LOGIN_SUBMIT 所以我不知道为什么以下失败但是......

driver.findElement(By.id("LOGIN_SUBMIT")).click();//failed  

编辑:

这是一个确切的示例(案例 1 of 2):

WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='gwt-debug-LOGIN_SUBMIT']")));
    //wait.until(ExpectedConditions.elementToBeClickable((By.id("gwt-debug-LOGIN_SUBMIT"))));  <!-- id works as well

好的,所以找到了元素。如果不是,它将超时并抛出异常。

尽管如此,以下失败(在Firefox下,与chrome一起使用)没有错误并且页面无法导航。

driver.findElement(By.xpath("//div[@id='gwt-debug-LOGIN_SUBMIT']")).click();
//driver.findElement(By.id("gwt-debug-LOGIN_SUBMIT")).click(); <-- fails too 

我要做的是:

driver.findElement(By.xpath("//div[@id='gwt-debug-LOGIN_SUBMIT']/div/table/tbody/tr[2]/td[2]/div/div/table/tbody/tr/td/div")).click();

所以我的经验是,即使我用xpath找到了元素,除非我使用完整的xpath,否则点击失败。

这是另一个确切的示例(案例 2 of 2):

我可以找到这样的元素:

WebElement we = driver.findElement(By.xpath("//*[@id=\"text" + i + "\"]"));

我知道我找到了它,因为我可以通过以下方式看到文本:

 we.getText();

仍然选择我发现它失败的路径。

//get outta town man the following fails
driver.findElement(By.xpath("//*[@id=\"text" + i + "\"]")).click();

在这种情况下,没有更明确的 xpath 可以尝试,如案例 1 我必须做的是使用 css:

//bingo baby works fine
driver.findElement(By.cssSelector("div#text" + i + ".myChoices")).click();

实际上,我通过 firebug 获得了 css 路径,而不是缩短了它。

//this is what I recieved
html.ext-strict body.ext-gecko div#x-auto-0.x-component div#x-auto-1.x-component div#x-auto-3..myBlank div#choicePanel1.myBlank div.x-box-inner div#text3.myChoices  //text3 is the id of the element I wanted to select

我不知道你是否能弄清楚你需要的 xpaths 和 css 选择器,但我相信我确实经历过你所做的。

于 2012-07-05T09:58:11.113 回答