应该发生的是,这些用于登录的文本字段应该被填充并且应该按下登录按钮(某种自动登录)
以下是该网页的链接:Telekom Email Login Page
public String exportDriver() throws IOException {
final InputStream IEDriverStream = getClass().getResourceAsStream("/Driver/IEDriverServer.exe");
final File ieDriverServer = FileWebOpener.stream2file(IEDriverStream, "IEDriverServer", ".exe");
System.setProperty("webdriver.ie.driver", ieDriverServer.getAbsolutePath());
return ieDriverServer.getAbsolutePath();
}
public void goToWebsite(String url) {
driver = new InternetExplorerDriver();
driver.get(url);
}
public void setUsernameAndPassword(String username, String password, int Website) throws InterruptedException {
try{ new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys(username);
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys(password);
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
} catch(Exception e){
e.printStackTrace();
}
}
我用C#测试了它并获得了正确的行为。以下是代码:
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System;
namespace IEWebDriver
{
class Program
{
static void Main(string[] args)
{
string username = args.Length > 1 ? args[1] : "test";
string password = args.Length > 1 ? args[2] : "test";
IWebDriver driver = new InternetExplorerDriver();
//IWebDriver driver = new ChromeDriver();
//Navigate to test URL
driver.Navigate().GoToUrl("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
try
{
new WebDriverWait(driver, new TimeSpan(0, 0, 20, 60)).Until(c => c.FindElement(By.CssSelector("input#user[name='pw_usr']"))).SendKeys(username);
driver.FindElement(By.CssSelector("input#pw_pwd[name='pw_pwd']")).SendKeys(password);
driver.FindElement(By.CssSelector("input.button.standard_button_size.large#pw_submit")).Click();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
//Close the browser
driver.Quit();
//driver.Close();
}
}
}
我相信你首先需要:
try- findElement(By.Class(“line_normalized clear relative”)。click);
这找到用户名textfield类并点击它如果我没有弄错,语法可能是错误但想法是相同的。同样适用于密码字段,找到字段的类/ ID名称并在发送密钥之前单击它。
您好,因为您想输入用户名并且它是输入字段,您需要找到输入标记。
您可以尝试以下定位器来定位文本输入区域并执行sendkeys操作:
driver.findElement(By.xpath("//div[@class='line_normalized clear relative']/input")).sendKeys("username");
我怀疑你的应用程序太慢了。使用WebDriverWait
识别元素然后Sendkeys
操作。
package pkg1;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testIE {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement user= wait.until(ExpectedConditions.elementToBeClickable(By.id("user")));
user.sendKeys("[email protected]");
WebDriverWait wait1 = new WebDriverWait(driver, 30);
WebElement pass= wait1.until(ExpectedConditions.elementToBeClickable(By.id("pw_pwd")));
pass.sendKeys("password");
}
}
在您最初访问website时,您需要引导WebDriverWait以使所需元素可单击,并且您可以使用以下任一Locator Strategies:
cssSelector
:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
xpath
:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='user' and @name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.xpath("//input[@id='pw_pwd' and @name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.xpath("//input[@class='button standard_button_size large' and @id='pw_submit']")).click();