Selenium 4.0 中的相對定位器是什麼?
Selenium 4.0 中的相對或友好定位器可透過元素的 tagname 屬性獲得。
above() - 關於指定元素,位於其上方的 Web 元素。
語法 -
driver.findElement(withTagName(“<<tagnamevalue>>”).above(element));
below() - 關於指定元素,位於其下方的 Web 元素。
語法 -
driver.findElement(withTagName(“<<tagnamevalue>>”).below(element));
toLeftof() - 關於指定元素,位於其左側的 Web 元素。
語法 -
driver.findElement(withTagName(“<<tagnamevalue>>”).toLeftOf(element));
toRightOf() - 關於指定元素,位於其右側的 Web 元素。
語法 -
driver.findElement(withTagName(“<<tagnamevalue>>”).toRightOf(element));
使用相對定位器的程式碼實現。
示例
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import static org.openqa.selenium.support.locators.RelativeLocator
.withTagName;
public class RelLocator {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String url = "https://tutorialspoint.tw/about/about_careers.htm";
driver.get(url);
// maximizing browser with maximize()
river.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement head_label = driver.findElement(By.cssSelector("li[class='heading']"));
// getting the link text just below head_label web element
String txt = driver.findElement(withTagName("a").below(head_label))
.getText();
System.out.println("The text below heading is " + txt);
WebElement write =
driver.findElement(By.xpath("//a[text()='Write for us']"));
// getting the heading just above Write for us web link
String txtabove = driver.findElement(withTagName("li").above(write))
.getText();
System.out.println("The text above link is " + txtabove);
WebElement searchinp =
driver.findElement(By.xpath("//input[@name='search']"));
// getting the search button to the right of edit box searchinp.
driver.findElement(withTagName("button").toRightOf(searchinp))
.click();
WebElement prntlnk =
driver.findElement(By.xpath("//a[@class=' hide-on-mobile']"));
// getting the previous page link to the left of prntlnk.
String prevlink =
driver.findElement(withTagName("a").toLeftOf(prntlnk))
.getText();
System.out.println("The text left of link is " + prevlink);
driver.close();
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP