2017-12-27 250 views
0

在Moneycontrol的website中,我無法在嘗試登錄時輸入用戶名和密碼。Selenium無法找到Webelement。在Moneycontrol的登錄頁面中,Selenium無法找到Webelementes

public void setUrl() throws IOException { 
    driver = new FirefoxDriver() 
    driver.get("http://www.moneycontrol.com/"); 
} 

public void Login() { 
    driver.findElement(By.xpath("//a[@title='Log In']")).click(); 
    //enter user name and password 

    driver.findElement(By.xpath("//div[@class='formbox']/div[1]/form/div[1]")).sendKeys("[email protected]"); 
    driver.findElement(By.xpath("//input[@id='pwd']")).sendKeys("[email protected]"); 

screenshot

回答

0

Email or User ID字段是一個內,所以在Email or User IDPassword字段調用sendKeys()必須切換到相應如下:

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='https://accounts.moneycontrol.com/mclogin/?d=2']"))); 
driver.findElement(By.xpath("//form[@id='login_form']//input[@class='textfield' and @id='email']")).sendKeys("[email protected]"); 
driver.findElement(By.xpath("//form[@id='login_form']//input[@class='textfield' and @id='pwd']")).sendKeys("[email protected]"); 
+1

謝謝!它工作正常。請解釋你是如何找到這個xPath的? 在Firefox中我無法找到此webelement,並且沒有src,值爲「'https://accounts.moneycontrol.com/mclogin/?d=2']」「。 我也不明白你是如何找到這個頁面上有iframe的,我在查找firepath時沒有看到它們。好心解釋,謝謝。 – Rajesh

+0

@Rajesh找到正確的'xpath'非常簡單。將你的'Firefox'降級到支持'Firebug'&'Firepath'的v53.x子級別,這清楚地顯示了'頂級內容'和相關的'框架'。不久,您將瞭解如何檢測「幀」。升級回'Firefox Quantum'或'Chrome'並通過'Dev Tools'輕鬆找到'Frames',這是找到任何元素的最佳途徑。在'MoneyControl'網站上,如果您遍歷DOM,您會發現帶有'src'的'