xpathを使用してn番目のサブ要素を識別する方法は?
次の方法でxpathを使用してn番目のサブ要素を識別できます-
-
インデックス付きの角かっこを追加します。
-
xpathでposition()メソッドを使用する。
例
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();
}
} 例
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();
}
} -
JavaScriptを使用して親の子要素を取得するにはどうすればよいですか?
以下は、JavaScriptを使用して親の子要素を取得するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> &
-
C ++でSTLを使用して配列の最大要素を見つける方法は?
ここでは、最大要素を見つける方法を説明します。したがって、配列が[12、45、74、32、66、96、21、32、27]の場合、max要素は96です。algorithm.hヘッダーファイルにあるmax_element()関数を使用して、最大要素。 例 #include<iostream> #include<algorithm> using namespace std; int main() { int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27}; int n = sizeo