Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XPath获取属性并使用value属性提供内部文本_Xml_Selenium Webdriver_Xpath - Fatal编程技术网

Xml 使用XPath获取属性并使用value属性提供内部文本

Xml 使用XPath获取属性并使用value属性提供内部文本,xml,selenium-webdriver,xpath,Xml,Selenium Webdriver,Xpath,给定这样的XML结构: <table> <thead> <tr> <th><div data-col="0">First Name</div></th> <th><div data-col="1">Last Name</div></th> <th>&l

给定这样的XML结构:

<table>
    <thead>
        <tr>    
            <th><div data-col="0">First Name</div></th>
            <th><div data-col="1">Last Name</div></th>
            <th><div data-col="2">Age</div></th>
            <th><div data-col="3">Id</div></th>
            <th><div data-col="4">City</div></th>
        </tr>
    </thead>
    <tbody>
        <tr data-row="0">
            <td data-col="0"><div>Ivan</div></td>
            <td data-col="1"><div>Ivanov</div></td>
            <td data-col="2"><div>35</div></td>
            <td data-col="3"><div>EFF-12218</div></td>
            <td data-col="4"><div>London</div></td>
        </tr>
    </tbody>
</table>

名字
姓
年龄
身份证件
城市
伊凡
伊万诺夫
35
EFF-12218
伦敦
  • 如何获取 第一要素
  • 如何获取此单元格中文本的值 (
    'EFF-12218'
    )是否使用
    数据列的查找值
  • 如何获取第一个元素的data col(其中text='id')的值
  • String colNumber=driver.findElement(By.xpath(“//div[text()='Id'])).getAttribute(“数据列”)

  • 我怎样才能使用找到的数据列的值来获得这个单元格中文本的值('EFF-12218')
  • driver.findElement(By.xpath(“//td[@data col=''“+colNumber+'])).getText()

  • 如何获取第一个元素的data col(其中text='id')的值
  • String colNumber=driver.findElement(By.xpath(“//div[text()='Id'])).getAttribute(“数据列”)

  • 我怎样才能使用找到的数据列的值来获得这个单元格中文本的值('EFF-12218')
  • driver.findElement(By.xpath(“//td[@data col=''“+colNumber+'])).getText()

    要检索文本为
    id
    数据列的值,可以使用以下任一选项:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    理想情况下,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    要检索有关
    data col=“3”
    的值EFF-12218,可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    理想情况下,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    要检索文本为
    id
    数据列的值,可以使用以下任一选项:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    理想情况下,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    要检索有关
    data col=“3”
    的值EFF-12218,可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      
    理想情况下,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下解决方案:

    • 使用
      XPATH

      print(driver.find_element_by_xpath("//table/thead//th/div[text()='Id']").get_attribute("data-col"))
      
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table/thead//th/div[contains(., 'Id')]"))).get_attribute("data-col"))
      
      data-col = driver.find_element_by_xpath("//table//th//div[text()='Id']").get_attribute("data-col")
      print(driver.find_element_by_xpath("//table//tbody/tr//td[@data-col='"+ data-col +"']/div").get_attribute("innerHTML"))
      
      data-col = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//th//div[contains(., 'Id')]"))).get_attribute("data-col")
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//table//tbody/tr//td[@data-col='"+ data-col +"']/div"))).get_attribute("innerHTML"))
      

    要使用属性
    数据列的值作为
    元素子元素的索引,请使用此XPath-1.0表达式

    //tbody/tr/td[@data-col=//thead/tr/th[div/text()='Id']/div/@data-col]
    
    其结果是

    EFF-12218

    如预期。
    顺便说一下,你第一个问题的答案如下

    //thead/tr/th[div/text()='Id']/div/@data-col
    

    这是第二个问题的一部分。

    要使用属性
    数据列的值作为
    元素子元素的索引,请使用此XPath-1.0表达式

    //tbody/tr/td[@data-col=//thead/tr/th[div/text()='Id']/div/@data-col]
    
    其结果是

    EFF-12218

    如预期。
    顺便说一下,你第一个问题的答案如下

    //thead/tr/th[div/text()='Id']/div/@data-col
    

    这是第二个问题的一部分。

    请参阅下面的回答。它将帮助您获取任何单元格的文本。您只需在xpath中更改行和列的值:

     WebDriver driver = new ChromeDriver();
     // to Get Text of ID( you can store into array/arraylist
    // By Changing the row column value in xpath //tbody/tr[row]/td[column]) you can get 
       //data of any cell of table
    List<WebElement> id_elements = driver.findElements(By.xpath("//tbody/tr/td[4]"));
    int noOfRow= id_elements.size();
    for(int i=0; i<=noOfRow;i++) {
        String id = driver.findElement(By.xpath("//tbody/tr["+i+"]/td[4]")).getText();
        System.out.println(id);
        }
    
    WebDriver=new ChromeDriver();
    //要获取ID的文本(可以存储到array/arraylist中
    //通过更改xpath//tbody/tr[row]/td[column])中的行-列值,可以获得
    //表中任意单元格的数据
    List id_elements=driver.findElements(By.xpath(“//tbody/tr/td[4]”);
    int noOfRow=id_elements.size();
    
    对于(int i=0;i请参考下面的响应,它将帮助您获取任何单元格的文本。您只需更改xpath中行和列的值:

     WebDriver driver = new ChromeDriver();
     // to Get Text of ID( you can store into array/arraylist
    // By Changing the row column value in xpath //tbody/tr[row]/td[column]) you can get 
       //data of any cell of table
    List<WebElement> id_elements = driver.findElements(By.xpath("//tbody/tr/td[4]"));
    int noOfRow= id_elements.size();
    for(int i=0; i<=noOfRow;i++) {
        String id = driver.findElement(By.xpath("//tbody/tr["+i+"]/td[4]")).getText();
        System.out.println(id);
        }
    
    WebDriver=new ChromeDriver();
    //要获取ID的文本(可以存储到array/arraylist中
    //通过更改xpath//tbody/tr[row]/td[column])中的行-列值,可以获得
    //表中任意单元格的数据
    List id_elements=driver.findElements(By.xpath(“//tbody/tr/td[4]”);
    int noOfRow=id_elements.size();
    
    对于(inti=0;完整的回答。非常完整的回答。