获取用户名元素文本。

huangapple go评论79阅读模式
英文:

How to get username element text

问题

I want to take the text of username from div class.

The html page:

<div id="gridview-1030" class="x-grid-view x-fit-item x-grid-view-default x-unselectable"
style="overflow: auto; margin: 0px; width: 549px; height: 218px;" tabindex="-1">
<table class="x-grid-table x-grid-table-resizer" border="0" cellspacing="0" cellpadding="0" style="width:549px;">
    <tbody>
        <tr class="x-grid-header-row">
            <th class="x-grid-col-resizer-gridcolumn-1019" style="width: 69px; height: 0px;"></th>
            <th class="x-grid-col-resizer-gridcolumn-1020" style="width: 69px; height: 0px;"></th>
            <th class="x-grid-col-resizer-gridcolumn-1021" style="width: 69px; height: 0px;"></th>
            <th class="x-grid-col-resizer-gridcolumn-1022" style="width: 69px; height: 0px;"></th>
        </tr>
        <tr class="x-grid-row x-grid-row-selected x-grid-row-focused">
            <td class="x-grid-cell x-grid-cell-gridcolumn-1019 x-grid-cell-first">
                <div class="x-grid-cell-inner" style="text-align: left;">username</div>
            </td>
            <td class="x-grid-cell x-grid-cell-gridcolumn-1020">
                <div class="x-grid-cell-inner" style="text-align: left;">firstname</div>
            </td>
            <td class="x-grid-cell x-grid-cell-gridcolumn-1021">
                <div class="x-grid-cell-inner" style="text-align: left;">lastname</div>
            </td>
            <td class="x-grid-cell x-grid-cell-gridcolumn-1022">
                <div class="x-grid-cell-inner" style="text-align: left;">full name</div>
            </td>
        </tr>
    </tbody>
</table>
</div>

I tried using the xpath like below but it didn't work:

user = driver.find_element(By.XPATH, "//div[@id='gridview-1030']/table/tbody/tr[2]/td[1]/div").text

I got an error saying:

no such element: Unable to locate element: {"method":"xpath","selector":"//div[@id='gridview-1030']/table/tbody/tr[2]/td[1]/div"}

Please help me fix it, thanks.

英文:

So I want to take the text of username from div class.

The html page:

&lt;div id=&quot;gridview-1030&quot; class=&quot;x-grid-view x-fit-item x-grid-view-default x-unselectable&quot;
style=&quot;overflow: auto; margin: 0px; width: 549px; height: 218px;&quot; tabindex=&quot;-1&quot;&gt;
&lt;table class=&quot;x-grid-table x-grid-table-resizer&quot; border=&quot;0&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; style=&quot;width:549px;&quot;&gt;
    &lt;tbody&gt;
        &lt;tr class=&quot;x-grid-header-row&quot;&gt;
            &lt;th class=&quot;x-grid-col-resizer-gridcolumn-1019&quot; style=&quot;width: 69px; height: 0px;&quot;&gt;&lt;/th&gt;
            &lt;th class=&quot;x-grid-col-resizer-gridcolumn-1020&quot; style=&quot;width: 69px; height: 0px;&quot;&gt;&lt;/th&gt;
            &lt;th class=&quot;x-grid-col-resizer-gridcolumn-1021&quot; style=&quot;width: 69px; height: 0px;&quot;&gt;&lt;/th&gt;
            &lt;th class=&quot;x-grid-col-resizer-gridcolumn-1022&quot; style=&quot;width: 69px; height: 0px;&quot;&gt;&lt;/th&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;x-grid-row x-grid-row-selected x-grid-row-focused&quot;&gt;
            &lt;td class=&quot; x-grid-cell x-grid-cell-gridcolumn-1019   x-grid-cell-first&quot;&gt;
                &lt;div class=&quot;x-grid-cell-inner &quot; style=&quot;text-align: left; ;&quot;&gt;username&lt;/div&gt;
            &lt;/td&gt;
            &lt;td class=&quot; x-grid-cell x-grid-cell-gridcolumn-1020   &quot;&gt;
                &lt;div class=&quot;x-grid-cell-inner &quot; style=&quot;text-align: left; ;&quot;&gt;firstname&lt;/div&gt;
            &lt;/td&gt;
            &lt;td class=&quot; x-grid-cell x-grid-cell-gridcolumn-1021   &quot;&gt;
                &lt;div class=&quot;x-grid-cell-inner &quot; style=&quot;text-align: left; ;&quot;&gt;lastname&lt;/div&gt;
            &lt;/td&gt;
            &lt;td class=&quot; x-grid-cell x-grid-cell-gridcolumn-1022   &quot;&gt;
                &lt;div class=&quot;x-grid-cell-inner &quot; style=&quot;text-align: left; ;&quot;&gt;full name&lt;/div&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;

</div>

I tried using the xpath like below but it didnt work

user = driver.find_element(By.XPATH, &quot;//*[@id=&quot;gridview-1030&quot;]/table/tbody/tr[2]/td[1]/div&quot;).text

I got an error saying:

no such element: Unable to locate element: {&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;//*[@id=&quot;gridview-1030&quot;]/table/tbody/tr[2]/td[1]/div&quot;}

Please help me fix it thanks.

答案1

得分: 0

未测试,因为没有相关的网址是不可能的,但以下应该有效:

    [..]
    ## 将以下内容添加到您的导入部分
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    [..]
    ## 定义驱动程序,然后
    wait = WebDriverWait(driver, 10)
    
    desired_info  = [x.text for x in wait.until(EC.presence_of_all_elements_located((By.XPATH, '&#39;//div[@id=&quot;gridview-1030&quot;]//tr/td[1]/div&#39;)))]

参见[Selenium文档](https://www.selenium.dev/documentation/)获取更多详细信息。
英文:

Did not test it, as it's impossible without the url in question, however the following should work:

[..]
## add the following to your imports
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
[..]
## define driver, then
wait = WebDriverWait(driver, 10)

desired_info  = [x.text for x in wait.until(EC.presence_of_all_elements_located((By.XPATH, &#39;//div[@id=&quot;gridview-1030&quot;]//tr/td[1]/div&#39;)))]

See Selenium documentation for more details.

huangapple
  • 本文由 发表于 2023年7月24日 20:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76754470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定