How Selenium webdriver locate element with a http response full of javascript

大憨熊 提交于 2019-12-14 02:01:46

问题


I use selenium webdriver to speed up my testing. In my work our website will redirect to paypal for user to finish payment. However, I cannot make selenium webdriver to locate the email and password input field on paypal.

The sample paypal URL : https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-07L974777B231831F#/checkout/login

A demo of my code may like this:

    WebDriver m_driver = new FirefoxDriver();
    String redirected_url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-0AT82163FM860854K#/checkout/login";

    m_driver.get(redirected_url);
    Thread.sleep(15*1000);
    WebElement we = m_driver.findElement(By.xpath(".//*[@id='email']"));
    we.sendKeys("login_email");
    we = m_driver.findElement(By.xpath(".//*[@id='password']"));
    we.sendKeys("login_password");
    we = m_driver.findElement(By.xpath(".//*[@id='btnLogin']"));
    we.click();

My problem:

With that code and on the paypal website, I always got an error message of 'no such element' found exception.

I can locate the element with firepath in firefox but I cannot make selenium webdriver work.

I know this error may be caused by the javascript in the whole page of the paypal login page. I just don't know how to handle this situation. Can you please help me with an answer?


回答1:


The reason you cannot locate those elements is the iframe. So, use switchTo method and switchTo iframe before started looking for element. Something like:

driver.switchTo().frame("injectedUl");
WebElement we = m_driver.findElement(By.xpath(".//*[@id='email']"));
...



来源:https://stackoverflow.com/questions/34011236/how-selenium-webdriver-locate-element-with-a-http-response-full-of-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!