selenium之xpath /css
xpath:
a'a @title=\"a'a\
//a[contains(text(),'product_08')]/ancestor::tr//input[@class='live-tv-overview-delete']
WebElement delConfirmBut = driver.findElement(By.xpath("//div[starts-with(@id,'confirmDialog')]/div/div/div[3]/button[1]"));
delConfirmBut.click();
Actions action = new Actions(driver);
action.moveByOffset(4, 5).click();
form/element[last()-n] 选择倒数第几个
form/element[position()<n]位置小于n的element
form/element[@attr_name>Number] form节点下所有带attr_name属性,且属性值大于number的element
form/element[@attr_name<Number]
path1|path2 匹配1和2
//form/a[contains(text(),'text')]
driver.findElement(By.xpath("//a[contains(text(),'aaa')]"));
driver.findElement(By.xpath("//a[text()='aaa']"));
driver.findElement(By.xpath(".//*[@id='buy_count']/../a[contains(.,'+')]"));
<input id="buy_count" type="text" disabled="disabled" readonly="readonly" value="1">
<a class="plus_proinfo" href="javascript:;">+</a>
driver.findElement(By.xpath("//a[contains(@id,'aaa')][1]"));
driver.findElement(By.xpath("//a[@id='123'][@value='sadcds']"));
//body[form] body/a
driver.findElement(By.xpath("//a[starts-with(@id,'aaa_')]"));
driver.findElement(By.xpath("//a[starts-with(@id,'aaa_') and contains(text(),'sds')]"));
driver.findElement(By.xpath("//a[ends-with(@id,'aaa_') or contains(text(),'sds')]"));
driver.findElement(By.xpath("//a/div[Last()-1]"));
driver.findElement(By.xpath("//a/div[@classnum<5]"));
driver.findElement(By.tagName("a").id("cdcd"));
driver.findElement(By.tagName("a")).findElement(By.id("sd"));
如果要继续找子节点呢?we.findElement(By.xpath("//p"),但是,这样写是不对的,其结果是找到全部p标签,正确的写法是 we.findElement(By.xpath(".//p"),表示当前WebElement下的p标签。
Css:
1.driver.findElement(By.cssSelector("#username")); 2.driver.findElement(By.cssSelector("标签名[属性名='属性值']")); 3. ^= driver.findElement(By.cssSelector("标签名[属性名^='xxx']")); 匹配属性值以xxx开头的元素 $= driver.findElement(By.cssSelector("标签名[属性名$='xxx']")); 匹配属性值以xxx结尾的元素 *= driver.findElement(By.cssSelector("标签名[属性名^='xxx']")); 匹配属性值包含xxx的元素