2014-02-13 87 views
2

伊瑪嘗試點擊所有目前在HTML如何在硒的webdriver點擊一個鏈接

http://www.flipkart.com/在這種電子元件 - 按類別>商店 - >

下面

是HTML

的樣品部分
<a class="" data-tracking-id="0_Samsung" href="/mobiles/samsung~brand/pr?sid=tyy,4io&amp;otracker=hp_nmenu_sub_electronics_0_Samsung">Samsung</a> 

<a class="" data-tracking-id="0_All Brands" href="/mobiles/pr?sid=tyy,4io&amp;otracker=hp_nmenu_sub_electronics_0_All%20Brands">All Brands</a> 


<a class="" data-tracking-id="0_All Brands" href="/laptops/pr?sid=6bo,b5g&amp;otracker=hp_nmenu_sub_electronics_0_All%20Brands">All Brands</a> 

使用以下

WebElement ul = driver.findElement(By.id("menu-electronics-tab-0-content")); 
     List<WebElement> lis = ul.findElements(By.tagName("li")); 
     System.out.println(""+lis.size()); 
     String[] linksText = new String[lis.size()]; 
     int index = 0; 
     for(WebElement element : lis){ 

      linksText[index] = element.getText(); 

     String string = linksText[index]; 

     if(string.contains(" ")){ 
      linksText[index]= string.substring(0, string.indexOf(" ")); 
     } 

      index++; 
     }  
       for(index =0; index < linksText.length; index++){ 
      String xpath = "//*[contains(@href,'%s')]"; 
      String y = linksText[index]; 
        String xpathOfElement = String.format(xpath, String.valueOf(y)); 
         System.out.println("Name 1 "+driver.findElement(By.xpath(xpathOfElement)).getText()); 
        driver.manage().timeouts().implicitlyWait(50000, TimeUnit.MILLISECONDS); 

          driver.findElement(By.xpath(xpathOfElement)).click(); 

當IAM點擊iam運行代碼它正在進行,直到我= 12,直到惠普後,它是 拋出一個錯誤。有83個元素出席。我需要點擊並導航回

Name 1 Mobiles 
Name 1 Bluetooth Headsets 
Name 1 Laptops 
Name 1 Apple 
Name 1 
FAILED: Electronic 
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with 
Command duration or timeout: 50.20 seconds 
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15' 
System info: host: 'VALUED-8JGOG5PH', ip: '192.168.1.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45' 
Session ID: 7d9304d9-fe1a-444d-b6b6-4669bfc46a33 
Driver info: org.openqa.selenium.firefox.FirefoxDriver 
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=26.0}] 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) 
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145) 
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554) 
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268) 
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:79) 
    at Examples.FlipkartTest2.Electronic(FlipkartTest2.java:64) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 
    at org.testng.TestRunner.privateRun(TestRunner.java:767) 
    at org.testng.TestRunner.run(TestRunner.java:617) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:240) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) 
    at org.testng.TestNG.run(TestNG.java:1057) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Element is not currently visible and so may not be interacted with 
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15' 
System info: host: 'VALUED-8JGOG5PH', ip: '192.168.1.4', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_45' 
Driver info: driver.version: unknown 
    at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous281959659031274025webdriver-profile/extensions/[email protected]/components/command_processor.js:8179) 
    at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous281959659031274025webdriver-profile/extensions/[email protected]/components/command_processor.js:10814) 
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous281959659031274025webdriver-profile/extensions/[email protected]/components/command_processor.js:10831) 
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/ADMINI~1/AppData/Local/Temp/anonymous281959659031274025webdriver-profile/extensions/[email protected]/components/command_processor.js:396) 

是否有任何替代方法,而不是href單擊鏈接。

+0

您是否嘗試過尋找通過'XPath'價值的要素是什麼? – Brian

+0

糾正我,如果IAM錯誤的xpath值將像「所有品牌」某些元素相同 – Learner

回答

2

這是給你一個可選的解決方案:

  1. 迭代的所有鏈接,並保存在一個列表他們<href>屬性的值。
  2. 迭代上一步中生成的列表,並將瀏覽器導航到每個URL。

這樣做,這樣可以確保您的代碼不依賴於你從原來的網頁的DOM閱讀WebElement對象,一旦你收集了所有的網址,您不再需要留在這個網頁上。

步驟#1:

迭代lis之前,添加ArrayList<String> urls = new ArrayList<String>();

步驟#2:

替換driver.findElement(By.xpath(xpathOfElement)).click();

隨着urls.add(driver.findElement(By.xpath(xpathOfElement)).getAttribute("href"));

第3步:

迭代lis後,導航到每個網址:

for (String url : urls) 
    driver.get("http://www.flipkart.com"+url); 
+0

謝謝,但得到一個鏈接錯誤org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「xpath」, 「selector」:「// * [contains(@ href,'Mixer/Juicer/Grinders')]」} 命令持續時間或超時時間:50.21秒 有關此錯誤的文檔,請訪問:http://seleniumhq.org /exceptions/no_such_element.html如何解決它? – Learner

+1

'Mixer/Juicer/Grinders'字符串在元素的文本中。在元素的href中,它是一個'mixer-juicer-grinders'字符串。所以在'contains(@ href,'Mixer/Juicer/Grinders')''中,或者將'href'改爲'innerHTML',或者將'Mixer/Juicer/Grinders'改爲'mixer-juicer-grinders'。 –

相關問題