简体   繁体   English

查找要在Selenium WebDriver中使用的HTML元素

[英]Finding HTML elements to use in Selenium WebDriver

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. 我尝试在Java类中使用Selenium WebDriver来查找该特定元素,然后在输入字段中自动添加所需量的该元素。

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. 我有一个HTML表,每一行指定元素的类型和用于将X添加到特定行中的元素数量的输入字段。

<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: 为了获得该元素,我在Java代码中尝试了以下操作,但是没有运气:

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, ie pageFlow? 当元素名称的一部分是对控制器的引用(即pageFlow)时,是否可能?

what if i wanted to retrieve the following element identified by 20% .... 如果我想检索以下20%标识的元素怎么办....

I have tried to get that one using xpath and cssSelector with no luck. 我试图使用xpath和cssSelector来获得那个,但是没有运气。 Any suggestions? 有什么建议么?

To return an element from a JavaScript result, you can use the following syntax (i used jQuery as simplification): 要从JavaScript结果返回元素,可以使用以下语法(我使用jQuery作为简化形式):

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: 您还可以传递在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. 在我看来,您可能想在XPath或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可能起作用。

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

* The contains function is not pure CSS. * contains函数不是纯CSS。 It's an addition provided by the Sizzle library. 它是Sizzle库提供的附加功能。 This may or may not work, depending on whether Selenium is using a built-in CSS library or injecting Sizzle. 这可能行不通,这取决于Selenium是使用内置CSS库还是注入Sizzle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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