简体   繁体   中英

Selenium Webdriver in Internet explorer

I have automated the login functionality using selenium webdriver with java and executed it with chrome driver.

It is working fine. Then i tried to execute it with internet explorer .

But I'm getting the " Unable to locate the element ".

How can I solve this issue?

Code:

/* Login without entering username */

@Test(priority = 0)

public void loginWithoutUsername() throws InterruptedException {

        int emptyUsernameRowNumber = 1;
        WebElement login = driver.findElement(By.xpath("//*[@id='main-section']/div/div/section[2]/div[1]/ul/li[1]");
        login.click();
        Thread.sleep(1000);
        WebElement username =  driver.findElement(By.xpath("//*[@id='emailtxt']");
        username.clear();
        username.sendKeys(getCellContent(0, emptyUsernameRowNumber));
        Thread.sleep(1000);
        WebElement password = driver.findElement(By.xpath("//*[@id='passwordtxt']");
        password.sendKeys(getCellContent(1, emptyUsernameRowNumber));
        WebElement continueButton = driver.findElement(By.xpath("//*[@id='main-section']/div/div/section[2]/div[3]/a/span");
        continueButton.click();     
}

HTML:

<head>
<body class="game-play game-login">
<div id="page">
<form id="form1" method="post" action="./">
<div class="aspNetHidden">
<script type="text/javascript">
<input id="email_hidden" name="email_hidden" value="2" type="hidden">
<input id="username_hidden" name="username_hidden" value="2" type="hidden">
<input id="profilepath_hidden" name="profilepath_hidden" value="2" type="hidden">
<div class="container fluid">
<div class="content-wrap">
<main id="main-section" class="transition page-in">
<div class="main-content">
<div class="game-login-wrap">
<section class="gme-ply-head">
<section class="center-pin">
<div class="title-head">
<ul class="log-reg clearfix">
<li class="active">Login</li>
<li>
</ul>
</div>
<div class="field-input">
<div class="login__row">
<input id="emailtxt" class="login__input email" name="emailtxt" placeholder="Email" type="text">
<span class="log-icon">
</div>
<div class="login__row scn-top">
<input id="passwordtxt" class="login__input pass" name="passwordtxt" placeholder="Password" type="password">
<span class="log-icon">
</div>
</div>

Try using ExplicitWait to check element is present or not and then do your actions

WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='main-section']/div/div/section[2]/div[1]/ul/li[1]")));

 // Do your actions

Its very often for the xpaths written in chrome to not work with internet explorer.

With the given HTML snippet and assuming the Login wrapped in 'li' tag as your login link, you can try using below xpath:

//li[text()='Login']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM