Chúng ta có thể xác định phần tử phụ thứ n bằng cách sử dụng XPath theo các cách sau - Show
Thí dụ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; public class SubElement { 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://www.tutorialspoint.com/index.htm"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // xpath using position() targeting the first element with type text driver.findElement(By.xpath("//input[@type='text'][position()=1]")) .click(); driver.close(); } } Thí dụ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; public class RowCount { 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://www.tutorialspoint.com/plsql/plsql_basic_syntax.htm"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // xpath with index appended to get the data from the row 2 of table List<WebElement> rows = driver.findElements(By.xpath("//table/tbody/tr[2]/td")); System.out.println(“The number of data in row 2 is “+ rows.size()); driver.close(); } }
Cập nhật vào ngày 10 tháng 6 năm 2020 13:06:19
Đã hỏi 12 năm, 7 tháng trước 12 years, 7 months ago Đã xem 91k lần 91k times XML đầu vào mẫu của tôi là:
Tôi giả sử chọn một nút Làm thế nào tôi có thể sử dụng giá trị của một biến để kiểm tra vị trí của một nút?
John Smith 7.0886 Huy hiệu vàng47 Huy hiệu bạc60 Huy hiệu Đồng6 gold badges47 silver badges60 bronze badges hỏi ngày 9 tháng 3 năm 2010 lúc 9:11Mar 9, 2010 at 9:11
Điều sau đây sẽ hoạt động:
Và nếu không, hãy thử:
Đã trả lời ngày 9 tháng 3 năm 2010 lúc 9:26Mar 9, 2010 at 9:26
Ronald Wildenbergronald WildenbergRonald Wildenberg 31.2K13 Huy hiệu vàng89 Huy hiệu bạc131 Huy hiệu đồng13 gold badges89 silver badges131 bronze badges 7 Làm thế nào tôi có thể có được đứa con thứ n trong XPath?Bằng cách thêm dấu ngoặc vuông với chỉ mục.Bằng cách sử dụng phương thức vị trí () trong XPath.. By using position () method in xpath.
Làm thế nào để bạn chọn phần tử thứ n trong selenium?Bạn có thể sử dụng Tag Tag: nth-of-Type (n).Nó sẽ chọn phần tử thẻ thứ n của danh sách.tag:nth-of-type(n)”. It will select the nth tag element of the list.
Làm thế nào để bạn chọn phần tử thứ hai có cùng XPath?Ví dụ: nếu cả hai trường văn bản đều có // nhập [@id = 'something'] thì bạn có thể chỉnh sửa trường đầu tiên xpath dưới dạng (// input [@id = 'something'])// nhập [@id = 'something']) [2] trong kho lưu trữ đối tượng.(//input[@id='something'])[2] in object repository.
Làm cách nào để tìm phần tử web sử dụng XPath?Làm thế nào để tìm các yếu tố của XPath trong selen: ví dụ.. Chuyển đến tab Tên đầu tiên và nhấp chuột phải >> Kiểm tra .. Khi kiểm tra phần tử web, nó sẽ hiển thị thẻ đầu vào và các thuộc tính như lớp và id .. Sử dụng ID và các thuộc tính này để xây dựng XPath, lần lượt, sẽ định vị trường tên đầu tiên .. |