Selenium Webdriver如何使用充满JavaScript的http响应定位元素 [英] How Selenium webdriver locate element with a http response full of javascript
问题描述
我使用Selenium Webdriver加快了测试速度.在我的工作中,我们的网站将重定向到Paypal,以便用户完成付款.但是,我无法使用Selenium Webdriver在Paypal上找到电子邮件和密码输入字段.
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.
我的代码演示可能像这样:
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();
我的问题:
使用该代码并在贝宝(Paypal)网站上,我总是收到一条错误消息找不到这样的元素".
With that code and on the paypal website, I always got an error message of 'no such element' found exception.
我可以在firefox中找到带有firepath的元素,但无法使Selenium Webdriver正常工作.
I can locate the element with firepath in firefox but I cannot make selenium webdriver work.
我知道此错误可能是由Paypal登录页面整个页面中的javascript引起的.我只是不知道如何处理这种情况.你能帮我一个答案吗?
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?
推荐答案
找不到这些元素的原因是 iframe
.因此,在开始查找元素之前,请使用switchTo方法和switchTo iframe.
像这样:
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']"));
...
这篇关于Selenium Webdriver如何使用充满JavaScript的http响应定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!