2015-04-02 23 views
0

我想獲得文本框工具提示(Gmail的創建帳戶密碼字段)的信息,但我無法處理這種情況。如何獲取gmail創建帳戶密碼字段ToolTip信息使用web驅動程序硒

我使用Selenium WebDriver(java)。請從下面的代碼,我用什麼:

WebDriver wd = new FirefoxDriver(); 
wd.navigate().to("https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Ftab%3Dwm&hl=en"); 
WebElement NewButton = wd.findElement(By.xpath(".//*[@id='Passwd']")); 
NewButton.click(); 
String Tooltip = NewButton.getText(); 
/*String Tooltip = NewButton.getAttribute("title"); // used this getattribute method also */ 
System.out.println(Tooltip); 

我用其他的方法也類似這樣的方法,但不能正常工作&沒有顯示在控制檯的任何錯誤也。

請看以下方法也:

//2nd method 

/*Actions ActionChains = new Actions(wd); 
WebElement NewButton = wd.findElement(By.xpath(".//*[@id='Passwd']")); 
ActionChains.clickAndHold(NewButton).build().perform(); 
System.out.println("hold"); 
String Tooltip = NewButton.getText(); 
String Tooltip = NewButton.getAttribute("title"); 
System.out.println("done"); 
System.out.println(Tooltip);*/ 

//3rd method 

/*String tooltip=wd.findElement(By.xpath(".//*[@id='Passwd']")).getAttribute("title"); 
System.out.println(tooltip); 
System.out.println("Done");*/ 

//4th method 

/*WebElement ToolTip = wd.findElement(By.xpath(".//*[@id='Passwd']")); 
System.out.println(ToolTip.getText()); 
System.out.println("Done");*/ 

回答

0

如果你想獲取提示,這將是下一個密碼字段可見,你必須得到該WebElement而不是密碼字段。該字段沒有標題屬性或文本值。您可以在DOM中的提示,如果您搜索:

<div class="jfk-bubble">...</div> 

代碼:

this.driver.get("copy your link here"); 
WebElement passwordField = this.driver.findElement(By.id("Passwd"); 
passwordField.click(); 

// Example: get the title of the tooltip 
WebElement tooltipTitle = this.driver.findElement(By.xpath("/html/body/div[2]/div[1]/div[1]/p/strong")); 

System.out.println("The title is: " + tooltipTitle.getText()); 

返回:

The title is: Password strength: 

這個提示是不是在DOM開頭,所以你必須單擊後才能設置WebElement,並使用WebDriverWait以避免因網絡連接速度慢等問題...

相關問題