简体   繁体   English

如何在Selenium-java中使用xpath获取表中禁用属性的大小

[英]how to get the size of disabled attribute in table with xpath in selenium-java

I am trying to get the amount of columns in a specific table, but there are disabled attribute - and i would like to know if it is possible to get the size of it without the disabled ones(the amount of the displayed columns in the table). 我正在尝试获取特定表中的列数量,但是有禁用的属性-我想知道是否有可能在没有禁用的属性的情况下获取它的大小(表中显示的列的数量)。

as you can see some of the tds are disabled in the attached picture. 如您所见,附图中的某些tds已禁用。 The command i tried to work with is: 我尝试使用的命令是:

driver.findElements(By.xpath("//*/table[@id='TABLE NAME]/tbody/tr[2]/td[@style='display:none;']")).size()

the issue is that the style='display:none;' 问题是style='display:none;' might not work in future tables because the value will be different for disabled tds. 可能在将来的表中不起作用,因为对于禁用的tds,该值将有所不同。 Thanks in advance. 提前致谢。

To return the visible ones, you could use not and contains in your XPath: 要返回可见的内容,可以在XPath中使用notcontains

driver.findElements(By.xpath("id('table-id')/tbody/tr[2]/td[not(contains(@style,'display:none'))]")).size()

Or with a CSS selector: 或使用CSS选择器:

driver.findElements(By.cssSelector("#table-id > tbody > tr:nth-child(2) > td:not([style*='display:none'])")).size()

Another way would be to filter all the elements with WebElement::isDisplayed : 另一种方法是使用WebElement::isDisplayed过滤所有元素:

driver.findElements(By.cssSelector("#table-id > tbody > tr:nth-child(2) > td"))
  .stream().filter(WebElement::isDisplayed).count()

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

相关问题 无法在Selenium-java中使用XPath错误找到元素 - Unable to locate an element using xpath error in selenium-java 如何在selenium-java中创建一个Coordinates实例? - How to create a Coordinates instance in selenium-java? 如何使用 selenium-java 点击 href 链接 - how to click in href link with selenium-java 使用 selenium-java 重新捕获器 - recaptcher with selenium-java "ChromeOption 不适用于 Selenium-Java" - ChromeOption is not working with Selenium-Java 如何在Selenium-Java 3.11中将DesiredCapabilities添加到GeckoDriver - How to add DesiredCapabilities to GeckoDriver in Selenium-Java 3.11 如何使用 Selenium-Java 进行自动化的可访问性测试? - How to do automated accessibility testing using Selenium-Java? 如何从Selenium-java中的JavaScript弹出式日历中选择日期 - How to select a date from javascript popup calendar in selenium-java 当禁用按钮时如何编写xpath:Selenium + Java + salesforce - How to write xpath when the button is disabled : Selenium + java + salesforce 对 TestNG version# 和 Selenium-JAVA version# 感到困惑 - 如何获取 Maven POM.xml 文件的版本号 - Confused about TestNG version# and Selenium-JAVA version# - How to get version number for Maven POM.xml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM