2017-12-27 234 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'的'