Selenium:如何点击 javascript 按钮 [英] Selenium: how to click on javascript button

查看:78
本文介绍了Selenium:如何点击 javascript 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为自动测试编写一些脚本,以检查使用 flex/amf 技术构建的 Web 应用程序的加载时间.测试将包括打开 IE 浏览器,浏览多个选项卡并测量从单击最后一个选项卡到加载页面内容然后关闭浏览器的时间.

I must write some scripts for automatic tests to check load-time a web application built in flex/amf technology. The test will consist in opening the IE browser, going through several tabs and measuring the time from clicking on the last tab to load the page content and then closing the browser.

我用 Java 编写了一个带有 Selenium Web Driver 和 Junit 的小脚本.脚本打开 IE 窗口,输入登录名和密码.我在点击"登录按钮时遇到问题.

I wrote in java a small script with Selenium Web Driver and Junit. Script opening the IE-window, enter login and password. I have problem with 'click on' login-button.

首先,我尝试通过 findElement 和 By.partiallinktext 查找并单击按钮,但是 selenium 通知我:无法找到带有部分链接文本的元素"(ctrl+f 在该站点上工作正常).

Firstly I have try to find and click button by findingElement and By.partiallinktext, but selenium informed me: "Unable to find element with partial link text" (ctrl+f works fine on that site).

我尝试使用 moveByOffset 鼠标单击并按下按钮(Robot 类 - 用密码填充字符串后的tab"和enter").所有这些都不起作用.

I tried clicking using moveByOffset mouse and by pressing buttons (class Robot - 'tab' and 'enter' after fill string with password). All of them doesn't work.

接下来我找到了 JavascriptExecutor - 我想,它可以解决我的问题,但是我应该如何使用这个类?

Next I found JavascriptExecutor - I thing, that it could be answer for my problem, but how I should use this class?

该网站上的按钮:

<button style="width: 120px;" onclick="javascript:logIn();"> Login </button>

我的java代码:

WebElement button = driver.findElement(By.partialLinkText("Login")); 
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript ("document.getElementByText(\"Login\")).click();", button); 

我没有太多的测试经验,所以我会很感激你的帮助.

I haven't a lot of experience with tests, so I will be grateful for your help.

推荐答案

不要通过 JavaScript.试试这个:

Don't go through JavaScript. Try this:

String xPath = "//button[contains(.,'Login')]";
driver.findElement(By.xpath(xPath))).click();

更好,但尚未测试:

// xPath to find a button whose text() (ie title) contains the word Login
String xPath = "//button[contains(text(),'Login')]";
driver.findElement(By.xpath(xPath))).click();

另请注意,https://sqa.stackexchange.com/ 包含有关 Selenium(等)的信息

Please also note that https://sqa.stackexchange.com/ has info on Selenium (etc.)

这篇关于Selenium:如何点击 javascript 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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