需要使用PYTHON SELENIUM的xpath访问HTML表中的值

时间:2021-12-07 20:27:53

I have a Web browser's app UI Table as shown like this...

我有一个Web浏览器的应用程序UI表,如下所示......

DATAPOINT           VALUE           STATUS
Output Voltage     95.0 VAC         Nominal
PS Input Voltage   15.0 VAC         Nominal
Inverter Status       Off           Nominal
Major Alarm        No Alarm         Nominal
Minor Alarm        No Alarm         Nominal

InnerHTML representation is something like this ...

InnerHTML表示是这样的......

tr[1] ---> td[1]      td[2]         td[3]
tr[2] ---> td[1]      td[2]         td[3]
tr[3] ---> td[1]      td[2]         td[3]
tr[4] ---> td[1]      td[2]         td[3]
tr[5] ---> td[1]      td[2]         td[3]

The above table is created by HTML Table like this ...

上面的表格是由HTML表格创建的......

<table id="inventoryGrid1" class="table table-striped table-bordered dataTable no-footer" role="grid" style="width: 408px;">
  <thead>
  <tbody>
    <tr class="odd" role="row">
      <td>Output Voltage</td>
      <td>95.0 VAC</td>
      <td>
        <img src="../img/led/led_green.png">
        <span> Nominal </span>
      </td>
    </tr>
    <tr class="even" role="row">
      <td>PS Input Voltage</td>
      <td>135.0 VAC</td>
      <td>
    </tr>
    <tr class="odd" role="row">
      <td>Inverter Status</td>
      <td>Off</td>
      <td>
    </tr>
    <tr class="even" role="row">
    <tr class="odd" role="row">
  </tbody>
</table>

The Full xpath is ...

完整的xpath是......

/html/body/div[2]/div/div/div/div[2]/div[2]/div/div/div/div[1]/div[1]/div/table/tbody/tr[1]/td[2]

I need to know ...

我需要知道 ...

  1. I can't assign:
    row2col3 = drv.find_element_by_xpath("/html/body/div[2]/div/div/div/div[2]/div[2]/div/div/div/div[1]/div[1]/div/table/tbody/tr[2]/td[3]") # attempting to assign a variable to element in the table( row 2 column 3) which is "Nominal"

    我无法分配:row2col3 = drv.find_element_by_xpath(“/ html / body / div [2] / div / div / div / div [2] / div [2] / div / div / div / div [1] / div [1] / div / table / tbody / tr [2] / td [3]“)#尝试将变量赋给表中的元素(第2行第3列),即”Nominal“

    Throws error: as 'list' object has no attribute ... Is it displaying that td tag name is a list ? and hence can't assign to a single variable ? Anyways, I am suppose to use row2col3 = drv.find_elements_by_xpath("/html/body/div[2]/div/div/div/div[2]/div[2]/div/div/div/div[1]/div[1]/div/table/tbody/tr[2]/td[3]") in order to get it working !

    引发错误:因为'list'对象没有属性...是否显示td标记名称是列表?因此无法分配给单个变量?无论如何,我想使用row2col3 = drv.find_elements_by_xpath(“/ html / body / div [2] / div / div / div / div [2] / div [2] / div / div / div / div [1] / div [1] / div / table / tbody / tr [2] / td [3]“)以使其正常工作!

  2. If I use the row2col3 element I neither can use row2col3.text nor row2col3.get_attribute("innerHTML"). So how do I get this ? Is it even possible ?

    如果我使用row2col3元素,我既不能使用row2col3.text也不能使用row2col3.get_attribute(“innerHTML”)。那么我怎么得到这个?它甚至可能吗?

How do I read out row2col3 particularly from the table ? Please forgive me for the long xpath implementation command and the bad english and also the long msg... :)

我如何从表中特别读出row2col3?请原谅我的长xpath实现命令和糟糕的英语以及长信息...... :)

1 个解决方案

#1


0  

value = findElement.byXpath("//tr[@class='even']//td[2]").getValue()

value = findElement.byXpath(“// tr [@ class ='even'] // td [2]”)。getValue()

#1


0  

value = findElement.byXpath("//tr[@class='even']//td[2]").getValue()

value = findElement.byXpath(“// tr [@ class ='even'] // td [2]”)。getValue()