[英]org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java
I have tried my best to write a login script in Selenium for the following site https://www.topmba.com/app . 我已尽力在Selenium中为以下站点https://www.topmba.com/app编写登录脚本。 Here is my code:
这是我的代码:
public class TopMba {
String driverPath = "/usr/bin/chromedriver";
WebDriver driver;
String username = "test@gmail.com"; // Change to your username and passwrod
String password = "12345";
// This method is to navigate topmba URL
@BeforeClass
public void init() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("https://www.topmba.com");
}
// To log in topmba
@Test
public void login() {
driver.findElement(By.className("tm-user")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//*[@id=\"tm-modal-frame-nvtfa7vvbm\"]")));
driver.findElement(By.id("edit-user")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().defaultContent();}
@AfterClass
public void quit() {
driver.close();
}
Here is the Exception : 这是例外:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="tm-modal-frame-nvtfa7vvbm"]"}
org.openqa.selenium.NoSuchElementException:否这样的元素:无法找到元素:{“方法”:“ xpath”,“选择器”:“ // * [@ id =“ tm-modal-frame-nvtfa7vvbm”]“}
Use the below code: 使用以下代码:
driver.get("https://www.topmba.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.className("tm-user")).click();
WebElement iframe = driver.findElement(By.xpath("//iframe[contains(@src,'app/sso/user/login')]"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("edit-name")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().window(parentWindowHandle);
There are a couple of things you need to take care as follows : 您需要注意以下几点:
To click on the icon with tooltip as Login you have used : 要单击带有工具提示的图标作为登录名,您已使用过:
driver.findElement(By.className("tm-user")).click();
If you look at the HTML this Locator Strategy identifies the element uniquely but for a more focused click you need to target the <span>
tag which is within an <a>
tag which is within the <li>
tag with the class
attribute you have used. 如果您查看HTML,此定位器策略会唯一地标识元素,但是要获得更集中的点击,您需要将
<span>
标签定位在<a>
标签内,而<a>
标签位于<li>
标签内,并具有class
属性用过的。 Of-coarse you have induce WebDriverWait . 粗略地讲,您可以诱导WebDriverWait 。
Once the Sign In Dialog Box opens up you will observe the login fields are within an <iframe>
. 登录 对话框打开后,您将看到登录字段位于
<iframe>
。 So you have to induce WebDriverWait for both the cases, once for the frame to be available and for the desired element to be clickable and you can use the following solution : 因此,对于两种情况,您都必须诱导WebDriverWait ,一次使框架可用并且使所需元素可单击,然后可以使用以下解决方案:
Code Block : 代码块:
System.setProperty("webdriver.gecko.driver", "C:\\\\path\\\\to\\\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://www.topmba.com"); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.tm-user>a.tmba-user>span.fa-img-icons.fa-img-user"))).click(); new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='/app/sso/user/login']"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-name"))).sendKeys("khawar"); driver.findElement(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-pass")).sendKeys("khawar"); driver.findElement(By.cssSelector("button.btn.btn-warning.btn-block.button.js-form-submit.form-submit#edit-submit")).click();
Browser Snapshot : 浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.