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

时间:2015-05-22 10:01:47

标签: 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 =&#34; signin&#34;不属于输入行。

  <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中没有方法按值读取元素,以便我可以读取值=&#34;登录&#34; 。我尝试通过Jsoup读取元素,转换为字符串并转换为Selenium Web元素,但是我收到错误。

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

我怎样才能使signInButton.click()工作?

2 个答案:

答案 0 :(得分:3)

为按钮指定ID,或使用其他选择器。例如:

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

答案 1 :(得分:2)

您不仅限于按ID查找元素。查看Selenium Api here以查看可用于定位元素的其他表单。

以下是使用XPath的可能解决方案。

WebElement sigInButton = driver.findElement(by.xpath(".//p[@id='signin']/input"));
signInButton.click();