
Xpath , CSS and Change Attribute
Xpath
-
Xpath with regular expression examples:
-
By.xpath("//*[contains(@id,'ZCS2_SV') or contains(@id,'ZCS2_CON_OFF')]")
-
By.xpath("//*[contains(@id,'ZCS2_SV') and contains(@id,'ZCS2_CON_OFF')]")
-
By.xpath("//*[contains(@id,'ZCS2_') and not(contains(@id,'MAIL'))]"))
-
The point (.) all the types( @id,@name )
By.xpath("//*[contains( . ,'put the text that you whant to find')]")
-
Closest to the root By.xpath("(//*[ @id ,'ZCS2_SV')])[1]")
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CSS
1
By.cssSelector("input[id='lst-ib']"
advance
1 and
By.cssSelector("input[id='lst-ib'][name='q']"
2 home שסוף המחרוזת מסתיים במילה $
לדוגמה
input[class$='home']
3 home שתחילת המחרוזת מסתיים במילה ^
4 home אם המחרוזת מכילה את המילה *
5 להיכנס לילדים >
לדוגמה
fieldset>table
6 id #
לדוגמה
fieldset>#table
דוגמה נוספת
List<WebElement> css3 = driver.findElements(By.cssSelector("article[class^='col tile tile-no-header tile-no-footer'][data-tracking-id^='&mcicid=Home'] a section figure"));
System.out.println("css elements is: " + css3.size());
CSS with table
CSS selection a column of a table
tr:nth-child(2) > td:nth-child(3)
CSS selection of first column of first row of a table
table.myTable > tbody > tr:first-child > td:first-child
More Ex.
tr:nth-child(2) > td:nth-child(3) >div >a
Change Attribute
WebElement ttt = driver.findElement(By.cssSelector("li[role=treeitem]:nth-of-type("+ i + ")"));
js.executeScript( "arguments[0].setAttribute('class','select2-results__option')",ttt);