使用Java的Selenium RC中的XPath或CSS不工作 [英] XPath or CSS in Selenium RC with Java is not working
问题描述
我尝试使用selenium RC自动执行以下情况:
- 打开Google首页,在搜索框中输入
-
- 点击搜索按钮。
由于我没有看到这些链接的名称或id属性,并且此链接的内容是动态的,我试图使用XPath或CSS。
从Firebug,我得到XPath和CSS通过右键单击然后复制XPath,复制CSS。
XPATH:/ html / body / div [2] / div / div / div [6] / div [2] / div / div [2] / div / ol / li [1] div / span / h3 / a
CSS:html body#gsr div#main div div#cnt div#nr_container div#center_col div#res.med div#search div#ires ol#rso li。 g div.vsc span.tl h3.r al
我试过在selenium IDE中进入XPath和查找按钮。它工作良好
但是当我使用上面的XPath或CSS在硒中RC作为:selenium.click xpath = // html / body / div [2] / div / div / div [6] / div [2] / div / div [2] / div / ol / li [1] / div / span / h3 / a );
selenium.click(css = html body#gsr div#main div div#cnt div#nr_container div#center_col div#res.med div#search div#ires ol#rso li.g div.vsc span。 tl h3.r al);
上述两行都不起作用,并给出错误。
请建议。
我的代码如下:
packages Eclipse_Package;
import com.thoughtworks.selenium。*;
import org.junit。*;
// import org.junit.Before;
// import org.junit.Test;
import java.util.regex.Pattern;
public class Selenium_SX extends SeleneseTestCase {
// public class Jun3
@Before
public void setUp(){
selenium = new DefaultSelenium(localhost,4444,* firefox3 C:\\Program Files(x86)\\Mozilla Firefox\\firefox.exe,http://www.google.co.in/ );
selenium.start();
}
// C:\Program Files(x86)\Mozilla Firefox \
@SuppressWarnings(deprecation)
@Test
public void test (){
selenium.open(http://www.google.com);
selenium.windowMaximize();
// selenium.waitForPageToLoad(5000);
// selenium.type(id = acpro_inp3,selenium);
selenium.type(q,software);
selenium.click(btnG);
selenium.waitForPageToLoad(7000);
// selenium.fireEvent(Selenium web application testing system,click);
// selenium.click(link = Selenium web application testing system);
// selenium.click(xpath = // html / body / div [2] / div / div / div [6] / div [2] / div / div [2] / div / ol / li [1] / div / span / h3 / a);
selenium.click(xpath =(// a [class = \li [1] \])[1]);
// selenium.click(css = // div ['span.tl h3.r a.l']);
selenium.waitForPageToLoad(15000);
}
@After
public void tearDown(){
selenium.stop();
}
// public static void main(String args [])throws Exception {
// Selenium_SX sx = new Selenium_SX()
// sx.setUp();
// sx.test();
// sx.tearDown();
//}
}
解决方案只是一个旁注, Google禁止自动查询。
您正在寻找的是以下内容:
// a [contains(text(),'software')]
这将在链接文本中选择具有软件的第一个链接。
I am trying to automate the following scenario with selenium RC:
- Open Google home page and enter "Software" in search box and then click on search button.
- Click on the first link of multiple links retrieved by Google search.
As I don't see either name or id attributes for these links and as this link's content is dynamic, I am trying to use XPath or CSS.
From Firebug, I got XPath and also CSS by right clicking then copy XPath, copy CSS.
XPATH:/html/body/div[2]/div/div/div[6]/div[2]/div/div[2]/div/ol/li[1]/div/span/h3/a CSS:html body#gsr div#main div div#cnt div#nr_container div#center_col div#res.med div#search div#ires ol#rso li.g div.vsc span.tl h3.r a.l
I tried entering above XPath in selenium IDE in target and find button. It worked fine but when I use the above XPath or CSS in selenium RC as:
selenium.click("xpath=//html/body/div[2]/div/div/div[6]/div[2]/div/div[2]/div/ol/li[1]/div/span/h3/a"); selenium.click("css=html body#gsr div#main div div#cnt div#nr_container div#center_col div#res.med div#search div#ires ol#rso li.g div.vsc span.tl h3.r a.l");
Both the above lines are not working and give an error. Please suggest.
My code is as below:
package Eclipse_Package; import com.thoughtworks.selenium.*; import org.junit.*; //import org.junit.Before; //import org.junit.Test; import java.util.regex.Pattern; public class Selenium_SX extends SeleneseTestCase { // public class Jun3 @Before public void setUp() { selenium = new DefaultSelenium("localhost", 4444, "*firefox3 C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", "http://www.google.co.in/"); selenium.start(); } // C:\Program Files (x86)\Mozilla Firefox\ @SuppressWarnings("deprecation") @Test public void test() { selenium.open("http://www.google.com"); selenium.windowMaximize(); // selenium.waitForPageToLoad("5000"); // selenium.type("id=acpro_inp3", "selenium"); selenium.type("q", "software"); selenium.click("btnG"); selenium.waitForPageToLoad("7000"); // selenium.fireEvent("Selenium web application testing system", "click"); // selenium.click("link=Selenium web application testing system"); // selenium.click("xpath=//html/body/div[2]/div/div/div[6]/div[2]/div/div[2]/div/ol/li[1]/div/span/h3/a"); selenium.click("xpath=(//a[class=\"li[1]\"])[1]"); // selenium.click("css=//div['span.tl h3.r a.l']"); selenium.waitForPageToLoad("15000"); } @After public void tearDown() { selenium.stop(); } // public static void main(String args[])throws Exception{ // Selenium_SX sx=new Selenium_SX(); // sx.setUp(); // sx.test(); // sx.tearDown(); // } }
解决方案Just a side note, Google has a ban on automated queries. I was using Google to test when I was new to testing, now I use our own server.
What you are looking for is this:
//a[contains(text(), 'software')]
This selects the first link with "software" in the link text.
这篇关于使用Java的Selenium RC中的XPath或CSS不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!