用selenium获取td类文本

时间:2017-09-04 11:40:53

标签: python selenium

所以我想采用td类的文本。

html页面

<table class="table table-striped">
    <tbody>
        <tr>
            <td class="text-center">
                <img .....>
            </td>
            <td>text</td>
            <td>text</td>
            <td class="text-center">
                <a ....></a>
            </td>
            <td class="text-center">
                TEXT I WANT TO TAKE HERE
            </td>
            <td class="text-center">
                <a ....><i class="fa fa-times"></i></a>
            </td>
        </tr>
    </tbody>
</table>

我想要的文字是#34;文字我想带到这里&#34;。

我尝试使用xpath,如下所示,但它没有工作

table = browser.find_element_by_xpath(("//div[@class='table table-striped']/tbody/tr/td[5]"));

我收到错误说:

no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='table table-striped']/tbody/tr/td[5]"}

是因为我在选择器中有多个类,我必须使用点吗? (我试过了:&#39; table.table-striped&#39;但它仍然没有用)

4 个答案:

答案 0 :(得分:3)

使用xpath下方获取文本

browser.find_element_by_xpath("//td[@class='text-center']").text

并使用索引以更好地找到您的行,例如

browser.find_element_by_xpath("//td[@class='text-center'][3]").text

答案 1 :(得分:2)

您的xpath不正确。您有一个<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>careData</groupId> <artifactId>SimpleMavenProj</artifactId> <version>0.0.1-SNAPSHOT</version> <name>DemoMavenProj</name> <description>DemoMavenProj</description> <properties> <suiteXmlFile>src/main/resources/smokesuite.xml</suiteXmlFile> <mavenVersion>4.0.0</mavenVersion> <selenium.version>3.4.0</selenium.version> <testng.version>6.9.9</testng.version> <POI.version>3.7</POI.version> <log4j.version>1.2.17</log4j.version> </properties> <dependencies> <!-- Selenium Dependency --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>${selenium.version}</version> </dependency> <!-- TestNG Dependency --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>${testng.version}</version> <scope>test</scope> </dependency> <!-- POI Dependency --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>${POI.version}</version> </dependency> <!-- Log4j Dependency--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <suiteXmlFiles> <suiteXmlFile>${suiteXmlFile}</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> </plugins> </build> </project> 代码,但是,您正在寻找D:\eclipse-workspace\SimpleMavenProj>mvn -version Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T01:09:06+05:30) Maven home: D:\eclipse-workspace\apache-maven-3.5.0\bin\.. Java version: 1.8.0_131, vendor: Oracle Corporation Java home: C:\Program Files\Java\jdk1.8.0_131\jre Default locale: en_IN, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows" D:\eclipse-workspace\SimpleMavenProj>echo %JAVA_HOME% C:\Program Files\Java\jdk1.8.0_131 代码。因此,您只需将table替换为div

div

答案 2 :(得分:1)

在XPath表达式中,您正在寻找div标签,但您的HTML没有。也许你正在寻找表格标签:

table = browser.find_element_by_xpath(("//table[@class='table table-striped']/tbody/tr/td[5]"));

答案 3 :(得分:1)

使用xpath下面的内容TEXT I WANT TO TAKE HERE

//table//tr/td[contains(text(), 'TEXT I WANT TO TAKE HERE')]

更新后的答案:您可以参考以下任何一种xpath来获取您的遗产。

//td[5]
  

OR

//table[@class='table table-striped']//td[5]
  

OR

//table[@class='table table-striped']/..//following-sibling::td[5]
  

OR

//td[@class='text-center'][3]