Need xpath locators for visible elements

I am trying to do a test for my site. Has problems in some kind of custom form. The trick is that the number of text fields in the form varies depending on the user's parameters (the code has disabled, but there is a style < displayed: none;> tag), so I'm trying to find a more flexible approach than finding each element one by one and filling out forms using blocks try / except.

I am using xpath locator

text_fields = driver.find_elements_by_xpath("//div[@class='form-line']/div[@class='form-inputs']/input[@type='text' and not(ancestor::div[@style='display: none;'])]")

The problem is that firebug finds only the needed elements, but when I use its selenium script, printing the list text_fieldsgives me all the elements, even without the < displayed: none;> tag

How can I get only visible items?

PS sorry for my bad english ^ _ ^

+3
source share
3 answers

You can get all form elements in the usual way, then iterate over the list and remove those elements that do not return true on is_displayed().

+4
source

Try the method contains():

text_fields = driver.find_elements_by_xpath(
  "//div[@class='form-line']/div[@class='form-inputs']/input[@type='text' and 
  not(ancestor::div[contains(@style, 'display: none;')])]")

An important part:

div[contains(@style, 'display: none;')]

Note that if the style contains a string display:none;or display:none, the selector will not match.

+1
source

I use the following and it works great.

self.assertTrue(driver.find_element_by_xpath("//div[@id='game_icons']/div/div[2]/div/a/img"))

This, of course, is for Selena and Python.

0
source

All Articles