Ruby Selenium Web Drive: How to Find Specific Element by Xpath Div Id and CSS Class

Ruby Selenium Web Driver: How to find child element by class name matching substring

You cannot use svg as a tag name in xpath expression.Try using below xpath,

"./li/a/*[name()='svg'][contains(@class,'deleteIcon')]"

Ruby- Selenium: Could we find an element by its 2 properties in parallel at the same time?

     <div id="select_a_boundary" class="dataset_select2">Homes name</div>

Selenium code:

@driver.find_element(:xpath, "//div[@id = 'select_a_boundary' and @class = 'dataset_select2']") 
@driver.find_element(:css, "div[id=select_a_boundary][class=dataset_select2]")
@driver.find_element(:css, "#select_a_boundary.dataset_select2")

Is it possible to locate element by partial id match in Selenium

You can apply an ends-with CSS selector:

By.cssSelector("[id$=default-create-firstname]")

Update

Some time went by since the answer was posted. Here some update from the linked mozilla developer page and comments below:

New use By.css instead of By.cssSelector

By.css("[id$=default-create-firstname]")

Also see the four possibilities of

  • beginning with
  • anywhere inside
  • anywhere inside regardless capitalization
  • end with

/* Internal links, beginning with "#" */
a[href^="#"] {
background-color: gold;
}

/* Links with "example" anywhere in the URL */
a[href*="example"] {
background-color: silver;
}

/* Links with "insensitive" anywhere in the URL,
regardless of capitalization */
a[href*="insensitive" i] {
color: cyan;
}

/* Links that end in ".org" */
a[href$=".org"] {
color: red;
}

Ruby Selenium Web Driver: How to count child element nodes of a specific node

There are some possible ways depending on how specific you want the XPath to be, f.e. if you simply want to count child elements of any name, then you can use * :

row.find_elements(:xpath => "*").length

and if you want to specifically count child elements of certain names i.e li and div :

row.find_elements(:xpath => "*[self::li|self::div]").length

Find elements using css selector/xpath of Selenium Webdriver with Ruby

FindElement(By.CssSelector("[dd:concept='TITLE']")) should work.

Ah, you're using ruby.

I did a bit of looking, you could try this:

find_element(:css, "[dd:concept='TITLE']")

Edit once again.

I would suggest escaping the : in the tag name dd:concept, like this:

find_element(:css, "[dd\:concept='TITLE']")

Find div element by multiple class names?

I don't think barak manos's answer has fully explained it.

Imagine we have few elements as the followings:

  1. <div class="value test"></div>
  2. <div class="value test "></div>
  3. <div class="first value test last"></div>
  4. <div class="test value"></div>

How XPath matches

  • Match only 1 (exact match), barak's answer

    driver.findElement(By.xpath("//div[@class='value test']"));
  • Match 1, 2 and 3 (match class contains value test, class order matters)

    driver.findElement(By.xpath("//div[contains(@class, 'value test')]"));
  • Match 1, 2, 3 and 4 (as long as elements have class value and test)

    driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"));

Also, in cases like this, Css Selector is always in favor of XPath (fast, concise, native).

  • Match 1

    driver.findElement(By.cssSelector("div[class='value test']"));
  • Match 1, 2 and 3

    driver.findElement(By.cssSelector("div[class*='value test']"));
  • Match 1, 2, 3 and 4

    driver.findElement(By.cssSelector("div.value.test"));


Related Topics



Leave a reply



Submit