2016-12-27 138 views
0

我正在使用Java Eclipse練習Selenium。我希望能夠使用Selenium登錄到Twitter。雖然我沒有看到任何與用戶名和密碼字段相關聯的元素。爲什麼沒有元素?我怎樣才能訪問這些領域?如何找到元素[For Selenium]

謝謝!

enter image description here

+0

可能的重複[如何在java的selenium webdriver中通過索引查找元素](http://stackoverflow.com/questions/27208872/how-to-find-an-element-by -index-in-selenium-webdriver-for-java) – Pushkraj

+2

「class」和「name」屬性有什麼問題? – Guy

回答

0

你可以像下面那樣使用xpath。這些表達式正在工作。

WebDriver driver = new FirefoxDriver(); 

driver.get("https://twitter.com/login"); 

driver.findElement(By.xpath("//div[@class='signin-wrapper']//input[@name='session[username_or_email]']")).sendKeys("Yourusername"); 
driver.findElement(By.xpath("//div[@class='signin-wrapper']//input[@name='session[password]']")).sendKeys("YourPassword"); 
driver.findElement(By.xpath("//button[contains(text(), 'Log in')]")).click(); 
+0

非常感謝!這真的很有用@Maninder! –

-1

請使用如下的的XPath。我們可以訪問它。

(// * [@名稱= '會議[username_or_email]'])[2]

+0

謝謝Ramesh!感謝你的幫助! –

+0

你測試了這個嗎?它會工作Girri –

0

你可以簡單地訪問這些領域與XPath

//input[@placeholder="Phone, email or username"] 
//input[@placeholder="Password"] 

讓我知道,如果出於某種原因你不能使用這些選擇器或發生任何例外情況

+0

謝謝安德森!感謝你的幫助! –

0

這裏是如何找到的XPath: 在Chrome中,單擊檢查感興趣的元素。您可以選擇從那裏複製XPath。附上圖片以供參考:enter image description here