2016-06-10 50 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 ")); 

:另外我不知道你的行为将在这里扮演什么角色。我怀疑有存在以获得悬停的细节。只要尝试一次忽略它们。

相关问题