简体   繁体   English

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

[英]Need to access a value in the HTML Table using xpath from PYTHON SELENIUM

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这样创建的...

<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列)中的元素分配一个变量,该变量为“名义”

    Throws error: as 'list' object has no attribute ... Is it displaying that td tag name is a list ? 引发错误:由于“列表”对象没有属性……是否显示td标签名称是列表? 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 ! 无论如何,我想使用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"). 如果我使用row2col3元素,则不能使用row2col3.text或row2col3.get_attribute(“ innerHTML”)。 So how do I get this ? 那我怎么得到这个呢? Is it even possible ? 可能吗?

How do I read out row2col3 particularly from the table ? 如何从表中读出row2col3? Please forgive me for the long xpath implementation command and the bad english and also the long msg... :) 请原谅我冗长的xpath实现命令,英语不佳以及msg ... :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM