最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Finding HTML elements to use in Selenium WebDriver - Stack Overflow

programmeradmin2浏览0评论

Using Selenium WebDriver in a java class where I try to find that specific element and then automatically add a needed amount of that element in the input field.

I have an HTML table with each row specifying a type of element and an input field used to add X to the amount of element in the specific row.

<tr>
  <td class="non-sortable-table">
    <input class="required text" type="text" value="0" name="plate_order{pageFlow.plateorders[0].numberOfPlates}" tabindex="25">
  </td>
  <td class="non-sortable-table">
    <span>20% - White plates</span>
  </td>
  ...
</tr>

I have tried the following in my Java code in order to get that element, but with no luck:

WebElement element = (WebElement) js.executeScript("return ${\"document.getElementsByName('plate_order{pageFlow.plateorders[0].numberOfPlates}')\"");

WebElement element = driver.findElement(By.ByName('plate_order{pageFlow.plateorders[0].numberOfPlates}'))

how could i retreive that element in order to edit its input? Is it possible when parts of the name of the element is a reference to a controller, i.e. pageFlow?

what if i wanted to retrieve the following element identified by 20% ....

I have tried to get that one using xpath and cssSelector with no luck. Any suggestions?

Using Selenium WebDriver in a java class where I try to find that specific element and then automatically add a needed amount of that element in the input field.

I have an HTML table with each row specifying a type of element and an input field used to add X to the amount of element in the specific row.

<tr>
  <td class="non-sortable-table">
    <input class="required text" type="text" value="0" name="plate_order{pageFlow.plateorders[0].numberOfPlates}" tabindex="25">
  </td>
  <td class="non-sortable-table">
    <span>20% - White plates</span>
  </td>
  ...
</tr>

I have tried the following in my Java code in order to get that element, but with no luck:

WebElement element = (WebElement) js.executeScript("return ${\"document.getElementsByName('plate_order{pageFlow.plateorders[0].numberOfPlates}')\"");

WebElement element = driver.findElement(By.ByName('plate_order{pageFlow.plateorders[0].numberOfPlates}'))

how could i retreive that element in order to edit its input? Is it possible when parts of the name of the element is a reference to a controller, i.e. pageFlow?

what if i wanted to retrieve the following element identified by 20% ....

I have tried to get that one using xpath and cssSelector with no luck. Any suggestions?

Share Improve this question edited Apr 3, 2013 at 5:47 Ripon Al Wasim 37.9k42 gold badges159 silver badges178 bronze badges asked Mar 15, 2012 at 13:19 Jes CherguiJes Chergui 1,3181 gold badge23 silver badges35 bronze badges 1
  • 1 "//tr/td[@class='non-sortable-table'][2]/span" – faramka Commented Mar 15, 2012 at 13:53
Add a ment  | 

2 Answers 2

Reset to default 4

To return an element from a JavaScript result, you can use the following syntax (i used jQuery as simplification):

RenderedWebElement webElement = (RenderedWebElement) ((JavascriptExecutor) webDriver).executeScript("return jQuery('input[name^=plate_order]').get(0);");

You can also pass an element which was previosly selected in Selenium:

((JavascriptExecutor) webDriver).executeScript("return doSomethingWithElement(arguments[0]);", webElement);

It looks to me like you might want to use a starts-with type operator in your XPath or CSS.

Something like:

XPath: //input[starts-with(@name, 'plate_order')]
CSS: input[name^='plate_order']

Similar things should work for the span, too (if you know the text):

XPath: //span[starts-with(., '20%')]
CSS/Sizzle*: span:contains('20%')

If you don't know the text, then something this xpath might work.

XPath: //input[starts-with(@name, 'plate_order]/../../td/span

* The contains function is not pure CSS. It's an addition provided by the Sizzle library. This may or may not work, depending on whether Selenium is using a built-in CSS library or injecting Sizzle.

发布评论

评论列表(0)

  1. 暂无评论