0

I'm trying to grab a checkbox with disabled="disabled" attribute using:

 List<WebElement> checkBox= driver.findElements(By.xpath("xpath")); 

Where "xPath" is one of:

(.//*[contains(@class,'wfm-statusbar')])[1]/descendant::input

(.//*[contains(@class,'wfm-statusbar')])[1]/descendant::*[@type='checkbox']

(.//*[contains(@class,'wfm-statusbar')])[1]/descendant::*[@disabled='disabled']

ALL of the above work when checked in FirePath and Console. Example proof:

enter image description here

However, when I run the xPath in code, it fails:

org.openqa.selenium.TimeoutException: Timed out after 15 seconds waiting for ...

If I search for a "normal" checkbox (without the disabled attribute and on the same page), code works. So I'm pretty sure it is this disabled="disabled" attribute that is to blame.

Simplified HTML:

<div class="wfm-statusbar">
  <div>
    <span>  some text</span>
    <span>
    <label>
           <input type="checkbox"  disabled="disabled" >
    </label>
</span>
  </div>
</div>

Any suggestions? Thanks.

Andrejs
  • 10,803
  • 4
  • 43
  • 48
  • Please explain "it fails"! – SiKing Jun 21 '16 at 16:03
  • edited (added the exception) – Andrejs Jun 21 '16 at 16:06
  • Have you tried a longer wait? – SiKing Jun 21 '16 at 16:09
  • @SiKing - I haven't as I find that 15 seconds is ample time to find the element. All other elements are found instantly with the exception of this case. Thanks for the suggestion. – Andrejs Jun 21 '16 at 16:16
  • You will have to post a lot more information! The "disabled" suggests the element is ... disabled. Selenium generally does not permit interacting with disabled elements. Are you trying to click on it? Did you force it to enabled somehow? – SiKing Jun 21 '16 at 16:18
  • The point is to enter the page and check that the checkbox is disabled. But I'm not even trying `.click()` or `isEnabled()` or anything as it is pointless because Selenium can't even locate in the first place. I'm only trying to "grab it". I'd be happy if it would, as I could then save it to `List boxes` and then do `if(boxes.size()<1){ some logic}` – Andrejs Jun 21 '16 at 16:25

2 Answers2

1

You could try a CSS selector but I don't expect that it will work if your XPath doesn't. As SiKing said, Selenium won't interact with disable or invisible elements.

System.out.println(driver.findElements(By.cssSelector("div.wfm-statusbar input[disabled='disabled']")).isEmpty());

This will return true if no elements are found.

You will likely have to access these elements using JS. You can find a question related to that here: How to use JavaScript with Selenium WebDriver Java.

You will want to use the same CSS Selector but using JS, as below.

document.querySelectorAll("div.wfm-statusbar input[disabled='disabled']");

This will return a collection of elements that you can do things with.


Here's a simple example of something similar to what you will want to use for JavascriptExecutor.

WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
List<WebElement> linksJs = (List<WebElement>) jse.executeScript("return document.querySelectorAll('a')");
System.out.println(linksJs.size());
Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • +1. Thanks for the JavaScript suggestion. Can't run JS script as I get `java.lang.ClassCastException: [bla bla] ... cannot be cast to org.openqa.selenium.JavascriptExecutor`. I've troubleshooted this issue but with no success. If I ever get it to work, I'll get back to this answer. – Andrejs Jun 22 '16 at 09:17
  • I added a quick example of JSE and how you will likely use it in this case. – JeffC Jun 22 '16 at 15:23
0

This should work and only will get the checkbox if it is a checkbox and is disabled.

(.//*[contains(@class,'wfm-statusbar')])[1]/descendant::*[@type='checkbox' and @disabled='disabled']
Cole Elam
  • 41
  • 4
  • There's no reason for this to work as it simply combines two non-working selectors into one. p.s. I tried it anyway and it doesn't work. – Andrejs Jun 21 '16 at 16:14
  • You said that both selectors worked when you checked them separately in firepath. You could try using `WebDriverWait wait = new WebDriverWait(driver, 10);` then do `WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.XPATH("the xpath you are testing")));` and see if it even is visible. – Cole Elam Jun 21 '16 at 16:22