我如何从没有id的html中读取元素

  • 本文关键字:html 读取 元素 id java selenium
  • 更新时间 :
  • 英文 :


我有以下html片段从中提取用户名和密码元素;然后发送用户名和密码,并点击提交按钮。

<form name="sign_in" action=https://www.amazon.in/ap/widget method="post">
  <input type="hidden" name="sessionId" value="276-3860748-2761707" />
  <input type="hidden" name="widgetToken" value="X2VuY29kaW5nPVVURjgmb3BlbmlkLmFzc29jX2hhbmRsZT1hbXpuX2Fzc29jaWF0ZXNfaW4mb3BlbmlkLmNsYWltZWRfaWQ9aHR0cCUzQSUyRiUyRnNwZWNzLm9wZW5pZC5uZXQlMkZhdXRoJTJGMi4wJTJGaWRlbnRpZmllcl9zZWxlY3Qmb3BlbmlkLmlkZW50aXR5PWh0dHAlM0ElMkYlMkZzcGVjcy5vcGVuaWQubmV0JTJGYXV0aCUyRjIuMCUyRmlkZW50aWZpZXJfc2VsZWN0Jm9wZW5pZC5tb2RlPWNoZWNraWRfc2V0dXAmb3BlbmlkLm5zPWh0dHAlM0ElMkYlMkZzcGVjcy5vcGVuaWQubmV0JTJGYXV0aCUyRjIuMCZvcGVuaWQubnMucGFwZT1odHRwJTNBJTJGJTJGc3BlY3Mub3BlbmlkLm5ldCUyRmV4dGVuc2lvbnMlMkZwYXBlJTJGMS4wJm9wZW5pZC5wYXBlLm1heF9hdXRoX2FnZT0wJm9wZW5pZC5yZXR1cm5fdG89aHR0cHMlM0ElMkYlMkZhZmZpbGlhdGUtcHJvZ3JhbS5hbWF6b24uaW4lMkYlM0ZfZW5jb2RpbmclM0RVVEY4JTI2KlZlcnNpb24qJTNEMSUyNiplbnRyaWVzKiUzRDAmc2hvd1Jtck1lPTE=:SkFuUUJZQW8zS0lhSm5RN3dsWUVlV0ttdmErZ3J2N3BjYW5naVFKTitOND06MQ==">
  <input type="hidden" name="path" value="/gp/associates/login/login.html" />
  <input type="hidden" name="query" value="returl=/gp/associates/join/landing/main.html&amp;retquery="/>
  <input type="hidden" name="action" value="sign-in" />
  <input type="hidden" name="mode" value="1" />
  <div class="formLabel">Email address</div>
  <div class="formInput"><input type="text" name="username" id="username" value="" class="text" /></div>
  <div class="formLabel">Password </div>
  <div class="formInput"><input type="password" name="password" id="password" value="" class="text" /></div>
  <input type="checkbox" name="rememberMe" value="true" >Stay Signed In<br>
  <p id="signin">
    <input type="image" src="https://images-na.ssl-images-amazon.com/images/G/31/associates/network/sign-in-md-pri._V138360034_.gif"  width="69" alt="Sign In" value="Sign In" height="22" border="0" />
    <a id="needHelp" href="http://affiliate-program.amazon.in/gp/associates/login/help.html">Need help?</a>
  </p>
<script id="fwicm-script" type="text/javascript" src="https://images-na.ssl-images-amazon.com/images/G/31/x-locale/common/login/fwcim._V342129342_.js"></script>
<script type="text/javascript">
    fwcim.profile()
</script>
</form>

我正在使用selenium firefox驱动程序。我的代码片段是-

FirefoxDriver driver = new FirefoxDriver();
WebElement emailField=driver.findElement(By.id("username"));
emailField.sendKeys(login);
WebElement passwordField=driver.findElement(By.id("password"));
passwordField.sendKeys(passwd); 
val signInButton = driver.findElement(By.id("signin"));
signInButton.click();

现在,最后一行代码不起作用,因为id="signin"不是输入行的一部分。

  <p id="signin">
    <input type="image" src="https://images-na.ssl-images-amazon.com/images/G/31/associates/network/sign-in-md-pri._V138360034_.gif"  width="69" alt="Sign In" value="Sign In" height="22" border="0" />
    <a id="needHelp" href="http://affiliate-program.amazon.in/gp/associates/login/help.html">Need help?</a>
  </p>

在FirefoxDriver中没有方法按值读取元素,这样我就可以读取value="Sign in "。我尝试通过Jsoup读取元素,转换为字符串并转换为Selenium Web元素,但我得到错误。

ClassCastException: java.lang.String cannot be cast to org.openqa.selenium.WebElement

我怎么可能使signInButton.click()工作?

为按钮指定一个ID,或者使用不同的选择器。例如:

driver.findElement(By.cssSelector("#signin input"));

您不限于通过id查找元素。在这里查看Selenium Api,了解其他可用于定位元素的表单。

下面是使用XPath的一个可能的解决方案。
WebElement sigInButton = driver.findElement(by.xpath(".//p[@id='signin']/input"));
signInButton.click();

相关内容

  • 没有找到相关文章

最新更新