开发者

XPath or CSS in Selenium RC with Java is not working

开发者 https://www.devze.com 2023-03-12 05:35 出处:网络
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.

I am trying to automate the following scenario with selenium RC:

  1. Open Google home page and enter "Software" in search box and then click on search button.
  2. 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]/di开发者_如何学Cv/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.


Try this xpath (//a[@class="l"])[1]


there must be pause ( pause (1) ) or waitForElementPresent between 2 clicks here is working sample for phpUnit. Strange, but clickAndWait does'n work

$this->open("/");
$this->type("q", "software");
$this->click("btnG");
for ($second = 0; ; $second++) {
    if ($second >= 60) $this->fail("timeout");
    try {
        if ($this->isElementPresent("//a[@class='l']")) break;
    } catch (Exception $e) {}
    sleep(1);
}

$this->click("//a[@class='l']");
0

精彩评论

暂无评论...
验证码 换一张
取 消