英文:
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:
<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>
</tr>
</tbody>
</div>
I tried using the xpath like below but it didnt work
user = driver.find_element(By.XPATH, "//*[@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":"//*[@id="gridview-1030"]/table/tbody/tr[2]/td[1]/div"}
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, ''//div[@id="gridview-1030"]//tr/td[1]/div')))]
参见[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, '//div[@id="gridview-1030"]//tr/td[1]/div')))]
See Selenium documentation for more details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论