Angusiasty
Angusiasty

Reputation: 367

Selenium chaining CSS selector

I've been trying to find element by chaining it with previous selector like this:

WebElement click = driver.findElement(By.cssSelector("td[data-container-for='NumberFrom'] input.k-formatted-value"));       
System.out.println(click.toString());

WebElement input1 = click.findElement(By.cssSelector(" ~ input"));
System.out.println(input1.toString());

But I'm getting:

An invalid or illegal selector was specified
Element info: {Using=css selector, value= ~ input} 

When I'm using xpath instead of css, or single css selector everything works just fine:

WebElement input2 = click.findElement(By.xpath("//following-sibling::input"));
System.out.println(input2.toString());

WebElement input3 = driver.findElement(By.cssSelector("td[data-container-for='NumberFrom'] input.k-formatted-value ~ input"));
System.out.println(input3.toString());

My HTML is:

<td role="gridcell" data-container-for="NumberFrom">
  <span class="k-widget k-numerictextbox" style="">
    <span class="k-numeric-wrap k-state-default">
      <input class="k-formatted-value k-input" type="text" tabindex="0" style="display: inline;" title="" aria-disabled="false" aria-readonly="false">
      <input class="k-input" type="text" name="NumberFrom" data-role="numerictextbox" role="spinbutton" style="display: none;" aria-valuemin="1" aria-valuenow="" aria-disabled="false" aria-readonly="false" data-bind="value:NumberFrom">
    </span>
  </span>
</td>

Is there something wrong with my input1 selector?

Upvotes: 2

Views: 2216

Answers (2)

Drew
Drew

Reputation: 66

The following selector works for me using your HTML.

input.k-formatted-value ~ input

Upvotes: 0

alecxe
alecxe

Reputation: 474001

There is no way to reference the current node in a CSS selector in selenium.

Either stay with XPath and the sibling axis or use the full CSS selector.

Upvotes: 1

Related Questions