2016-06-10 103 views
0

我試圖獲得在搜索結果的工具提示文本中www.bigbasket.com,並用下面的代碼如何使用Selenium WebDriver獲取工具提示文本?

@FindAll({@FindBy(xpath="//*[contains(@id,'product')][not(contains(@style,'display:none'))]/div/span[2]/a")}) 
    List<WebElement> lblProductName; 

public String verifySearchResults(WebDriver browser, String productName){ 
     System.out.println(lblProductName.size()); 
     try{ 
      Actions actions = new Actions(browser); 
      for(WebElement eachElement:lblProductName){ 
       System.out.println(eachElement.getAttribute("href")); 
       actions.moveToElement(eachElement).perform(); 
       //Actions action = new Actions(driver); 
       actions.moveToElement(eachElement).build().perform(); 
       WebElement toolTipElement = browser.findElement(By.cssSelector(".uiv2-tool-tip-hover ")); 
       System.out.println("Tooltip text is "+toolTipElement.getAttribute("textContent")); 
      } 
     }catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 
     return productName; 
    } 

但與上面的代碼,我就能得到的只有工具提示文本的第一個搜索結果。您能否幫助我獲得所有搜索結果的工具提示文本?

手冊要遵循的步驟 1.轉到www.bigbasket.com 2.點擊跳轉和瀏覽按鈕 3.搜索蘋果 4.將鼠標移到每個搜索結果,並查看工具提示文本

+0

'actions.moveToElement(eachElement).perform(); //動作動作=新動作(驅動程序);'似乎毫無用處..是否存在或者是一個錯字? – nullpointer

+0

請分享您迄今爲止得到的輸出結果,您可能實際上期待着。 – nullpointer

回答

0

有一件事我可以觀察到的是,有多個tool-tip-hover元素,其中,在您使用:

WebElement toolTipElement = browser.findElement(By.cssSelector(".uiv2-tool-tip-hover ")); //this shall always access the same element 

相反,你應選擇使用此作爲

List<WebElement> toolTipElement = browser.findElements(By.cssSelector(".uiv2-tool-tip-hover ")) 

OR

我建議建立一個母公司和進一步訪問其子(你可以把它的代碼流相應):

WebElement parentImgTitle = driver.findElements(By.cssSelector(".uiv2-list-box-img-title"); 
WebElement childTooltip = parentImgTitle.findElement(By.cssSelector(".uiv2-tool-tip-hover ")); 

:另外我不知道你的行爲將在這裏扮演什麼角色。我懷疑有存在以獲得懸停的細節。只要嘗試一次忽略它們。

相關問題