Selenium WebDriver - Java - 单击按钮 [英] Selenium WebDriver - Java - Click a button

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

问题描述

我正在尝试点击一些按钮,我正在使用twitch上的Follow按钮作为示例。

I'm trying to click some buttons and I'm using the Follow button on twitch as an example.

我使用Selenium IDE尝试获取按钮的xpath。我得到的xpath是: // span [@ id ='ember637'] / a / span

I used the Selenium IDE to try to get the xpath for the button. What I got as xpath was: //span[@id='ember637']/a/span

如果我去FireFox并为按钮复制唯一选择器,我会得到: .js-follow> span:nth-​​child(1)

If I go to FireFox and copy unique selector for the button I get: .js-follow > span:nth-child(1)

我在java程序中尝试了两种方法但它们都不起作用。当我使用 // span [.. xpath时,我收到以下错误:

I have tried both in the java program and they don't work. When I use the //span[.. xpath I get the following error:

"Unable to locate a node using //span[@id='ember637']/a/span"

编辑:

我想点击按钮的网站示例(关注按钮):
http://www.twitch.tv/mradder89/profile/

Example of a site with a button I want to click(The Follow button): http://www.twitch.tv/mradder89/profile/

我使用的Selenium jar文件是selenium-server-standalone-2.35.0.jar

The Selenium jar file I am using is "selenium-server-standalone-2.35.0.jar"

我得到的错误是

"Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //span[@id='ember637']/a/span"

编辑2:

我下载了PhantomJSDriver exe文件(phantomjs.exe)并试用了它。它不起作用...
我没有像以前那样收到错误消息(无法找到节点......错误)。

I downloaded the PhantomJSDriver exe file(phantomjs.exe) and was trying it out. It doesn't work... I do not get a error message like before(the 'Unable to locate a node...' error).

以下是代码: http://pastebin.com/GzvubMZr

推荐答案

使用PhantomJSDriver,尝试不同的定位器。发布例外(如果有),否则发布最新信息,例如位置,文本等。

With PhantomJSDriver, try different locators. Post exceptions if there are any, otherwise post the elemenst's info, e.g. location, text, etc.

driver.findElement(By.xpath("//*[contains(@class, 'profile-actions')]//span[text()='Follow']")).click();
driver.findElement(By.cssSelector(".profile-actions .primary_button > span")).click();

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

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