簡體   English   中英

XPath 或 CSS 在 Selenium RC 與 ZD52387880E1EA22817A972D37592 不工作

[英]XPath or CSS in Selenium RC with Java is not working

我正在嘗試使用 selenium RC 自動化以下場景:

  1. 打開谷歌主頁,在搜索框中輸入“軟件”,然后點擊搜索按鈕。
  2. 單擊 Google 搜索檢索到的多個鏈接中的第一個鏈接。

由於我看不到這些鏈接的名稱或 id 屬性,並且此鏈接的內容是動態的,因此我嘗試使用 XPath 或 CSS。

從 Firebug,我得到 XPath 和 CSS 通過右鍵單擊然后復制 XPath,復制 Z2C56C360580485DFBED2424

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

我嘗試在目標中的 selenium IDE 中輸入 XPath 上方並找到按鈕。 它工作正常,但是當我在 selenium RC 中使用上述 XPath 或 CSS 時:

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");

以上兩條線都不起作用並給出錯誤。 請建議。

我的代碼如下:

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();
//      }
    }

順便說一句,谷歌禁止自動查詢 我剛開始測試時使用谷歌進行測試,現在我使用我們自己的服務器。

您正在尋找的是:

//a[contains(text(), 'software')] 

這將選擇鏈接文本中帶有“軟件”的第一個鏈接。

試試這個 xpath (//a[@class="l"])[1]

兩次點擊之間必須有暫停( pause (1) )或 waitForElementPresent 這里是 phpUnit 的工作示例。 奇怪,但是 clickAndWait 不起作用

$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']");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM