Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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
C# 从输入类中的元素中提取文本_C#_Selenium - Fatal编程技术网

C# 从输入类中的元素中提取文本

C# 从输入类中的元素中提取文本,c#,selenium,C#,Selenium,因此,我目前正在Visual Studio中使用C#和Selenium进行自动化测试。所以我的问题如下。我要从中提取文本的元素是: <input class="showroomprice pricehistory" type="text" data-showroomprice="100000" value="100.000" data-tooltip="true" data-html="true" data-original-title="<div class=&quot;p

因此,我目前正在Visual Studio中使用C#和Selenium进行自动化测试。所以我的问题如下。我要从中提取文本的元素是:

<input class="showroomprice pricehistory" type="text" data-showroomprice="100000" value="100.000" data-tooltip="true" data-html="true" data-original-title="<div class=&quot;price-history-popup&quot;>
<table>
    <thead>
        <tr>
            <th>
                Datum
            </th>
            <th>
                Showroom
            </th>
        </tr>
    </thead>
    <tr>
        <td>
            22 feb 2017
        </td>
        <td>
            € 100.000
        </td>
    </tr>
    <tr>
        <td>
            27 jul 2016
        </td>
        <td>
            € 399.999
        </td>
    </tr>
    <tr>
        <td>
            20 jan 2016
        </td>
        <td>
            € 333.333
        </td>
    </tr>
    <tr>
        <td>
            20 jan 2016
        </td>
        <td>
            € 123
        </td>
    </tr>
    <tr>
        <td>
            20 jan 2016
        </td>
        <td>
            € 12.333
        </td>
    </tr>
</table>
</div>">

现在您可以看到,div和table是输入类的一部分(请参阅最后一行中div之后的“>)。当我逐类查找元素showroomprice pricehistory并尝试获取文本时,我得到了一个空值。我也无法按div类查找元素

所以我需要的是,我需要创建一个包含表中所有文本的字符串。有什么想法吗


谢谢!

编写一个循环来连接来自所有单元格的文本,示例java代码

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WebTableExample 
{
    public static void main(String[] args) 
    {
        WebDriver driver = new InternetExplorerDriver();
        driver.get("test url");      
        string tableText;
        WebElement table_element = driver.findElement(By.id("yourtable id"));
        List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr"));

        int row_num,col_num;
        row_num=1;
        for(WebElement trElement : tr_collection)
        {
            List<WebElement> td_collection=trElement.findElements(By.xpath("td"));

            col_num=1;
            for(WebElement tdElement : td_collection)
            {
             // making all cells text to tableText
                tableText = tableText + tdElement.getText();

                col_num++;
            }
            row_num++;
        } 
    }
}
import java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.ie.InternetExplorerDriver;
公共类WebTableExample
{
公共静态void main(字符串[]args)
{
WebDriver驱动程序=新的InternetExplorerDriver();
获取(“测试url”);
字符串表格文本;
WebElement table_element=driver.findElement(By.id(“yourtable id”);
List tr_collection=table_element.findElements(By.xpath(“id('testTable'))/tbody/tr”);
int row_num,col_num;
行数=1;
for(WebElement-treelement:tr_集合)
{
List td_collection=treelement.findElements(By.xpath(“td”);
col_num=1;
for(WebElement-tdElement:td_集合)
{
//将所有单元格文本转换为表格文本
tableText=tableText+tdElement.getText();
col_num++;
}
行数++;
} 
}
}

您想从哪个元素中提取文本?
输入
表格
?呈现的HTML是否与上面一样,在输入
原始数据标题
属性中将div+表格作为字符串出现?我认为这不会起作用,因为表格实际上是字符串的一部分,是
数据原始标题的一部分输入的code>属性
在这种情况下更简单,提取属性值并将所有TD、TR标记替换为空白