我有一个动态表,其中一个列/字段的标题为“引用ID”。该表是动态的,但列不是动态的。我想获取所有参考ID作为字符串。
下面是字符串“参考ID”所在的单元格的摘要。如果单击此单元格,它将以升序或降序对动态表进行排序(取决于单击的次数)。
编辑:在下面包含了整个表头
<th tabindex="0" scope="col" id="NUMBER_headerCell_2" aria-sort="none" class="PagingGridLayout---align_start headCell_2" abbr="Reference ID">
<div id="NUMBER_column2_header" class="GridHeaderCell---grid_header_text">Reference ID<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
我检查了代码并复制了Xpath。我试图查看是否可以单击参考ID单元格,以便对列表进行排序。
reports = browser.find_element_by_xpath("//*[@id='NUMBER_headerCell_2']")
reports.click()
但是,我得到一个错误:
NoSuchElementException:消息:没有这样的元素:无法找到 元素:
我在这里有点挠头。不知道该怎么办。任何帮助将非常感激。 :)
<thead>
<tr>
<th class="PagingGridLayout---checkbox">
<div class="CheckboxGroup---choice_group CheckboxGroup---no_label CheckboxGroup---align_start" role="group">
<div class="CheckboxGroup---choice_wrapper">
<div class="CheckboxGroup---choice_pair">
<input id="NUMBER_headerCell_selection_0" type="checkbox" value="0">
<label for="NUMBER_headerCell_selection_0" class="CheckboxGroup---choice_label"><span class="CheckboxGroup---accessibilityhidden">Select all rows</span></label>
</div>
</div>
</div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_0" aria-sort="none" class="PagingGridLayout---align_start headCell_0" abbr="Task Name">
<div id="NUMBER_column0_header" class="GridHeaderCell---grid_header_text">Task Name<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_1" aria-sort="descending" class="PagingGridLayout---align_start headCell_1" abbr="Created On">
<div id="NUMBER_column1_header" class="GridHeaderCell---grid_header_text">Created On<span class="GridHeaderCell---accessibilityhidden">Sortable column, sorted descending, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_2" aria-sort="none" class="PagingGridLayout---align_start headCell_2" abbr="Reference ID">
<div id="NUMBER_column2_header" class="GridHeaderCell---grid_header_text">Reference ID<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_3" aria-sort="none" class="PagingGridLayout---align_start headCell_3" abbr="Assigned">
<div id="NUMBER_column3_header" class="GridHeaderCell---grid_header_text">Assigned<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_4" aria-sort="none" class="PagingGridLayout---align_start headCell_4" abbr="Status">
<div id="NUMBER_column4_header" class="GridHeaderCell---grid_header_text">Status<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_5" aria-sort="none" class="PagingGridLayout---align_start headCell_5" abbr="Task">
<div id="NUMBER_column5_header" class="GridHeaderCell---grid_header_text">Task<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_6" aria-sort="none" class="PagingGridLayout---align_start headCell_6" abbr="Date">
<div id="NUMBER_column6_header" class="GridHeaderCell---grid_header_text">Date<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_7" aria-sort="none" class="PagingGridLayout---align_start headCell_7" abbr="Delivery">
<div id="NUMBER_column7_header" class="GridHeaderCell---grid_header_text"> Delivery<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
<th tabindex="0" scope="col" id="NUMBER_headerCell_8" aria-sort="none" class="PagingGridLayout---icon PagingGridLayout---center headCell_8" abbr="">
<div id="NUMBER_column8_header" class="GridHeaderCell---grid_header_text"><span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div>
</th>
</tr>
</thead>
编辑2:我认为表格在字段中:
<div class="FieldLayout---field_layout"><div class="FieldLayout---accessibilityhidden"><span class="FieldLayout---field_label" id="303211e777b0011ec20d46df049f3362"></span></div><div class="FieldLayout---input_below"><div><div class="PagingGridLayout---scrollable_content"><table class="PagingGridLayout---table PagingGridLayout---scrollable PagingGridLayout---striped" aria-labelledby="303211e777b0011ec20d46df049f3362"><thead><tr><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_0" aria-sort="none" class="PagingGridLayout---align_start headCell_0" abbr="Task Name"><div id="303211e777b0011ec20d46df049f3362_column0_header" class="GridHeaderCell---grid_header_text">Task Name<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_1" aria-sort="ascending" class="PagingGridLayout---align_start headCell_1" abbr="Created On"><div id="303211e777b0011ec20d46df049f3362_column1_header" class="GridHeaderCell---grid_header_text">Created On<span class="GridHeaderCell---accessibilityhidden">Sortable column, sorted ascending, activate to sort descending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_2" aria-sort="none" class="PagingGridLayout---align_start headCell_2" abbr="Reference ID"><div id="303211e777b0011ec20d46df049f3362_column2_header" class="GridHeaderCell---grid_header_text">Reference ID<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_3" aria-sort="none" class="PagingGridLayout---align_start headCell_3" abbr="Assigned To"><div id="303211e777b0011ec20d46df049f3362_column3_header" class="GridHeaderCell---grid_header_text">Assigned To<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_4" aria-sort="none" class="PagingGridLayout---align_start headCell_4" abbr="Status"><div id="303211e777b0011ec20d46df049f3362_column4_header" class="GridHeaderCell---grid_header_text">Status<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_5" aria-sort="none" class="PagingGridLayout---align_start headCell_5" abbr="Task Type"><div id="303211e777b0011ec20d46df049f3362_column5_header" class="GridHeaderCell---grid_header_text">Task Type<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_6" aria-sort="none" class="PagingGridLayout---align_start headCell_6" abbr="Due Date"><div id="303211e777b0011ec20d46df049f3362_column6_header" class="GridHeaderCell---grid_header_text">Due Date<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_7" aria-sort="none" class="PagingGridLayout---align_start headCell_7" abbr="State Delivery Region"><div id="303211e777b0011ec20d46df049f3362_column7_header" class="GridHeaderCell---grid_header_text">State Delivery Region<span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th><th tabindex="0" scope="col" id="303211e777b0011ec20d46df049f3362_headerCell_8" aria-sort="none" class="PagingGridLayout---icon PagingGridLayout---center headCell_8" abbr=""><div id="303211e777b0011ec20d46df049f3362_column8_header" class="GridHeaderCell---grid_header_text"><span class="GridHeaderCell---accessibilityhidden">Sortable column, activate to sort ascending</span></div></th></tr></thead><tbody><tr class=""><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/task/5654916" class="elements---global_a">Review Detailed Design</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">20/06/2019</p></td><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/page/home/record/lQBVF6bMMOMdjxUR6YqrUD8u9p8k-hS8unK9V9i437Bur4yN7zdzNRJSMWAdApXIql8ccBvoZkua3wEEaLPwmfrIt4hML7WksA3M31Jis9t50K6rVQ/view/summary" class="elements---global_a">ROR000000003458</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">RD Demand Deployment Design</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">In Progress</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">EE ORD</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">04/07/2019</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">VIC-TAS</p></td><td class=""><div data-thumbnail="false" class="ImageGroup---image_gallery ImageGroup---default_direction ImageGroup---center"><img class="DocumentImage---image DocumentImage---icon" src="https://nbnco.appiancloud.com/suite/rest/a/content/latest/ioBVF6bMMOMdjxUR68quAKs_DZLlxmi4D-ulyeD5psPvbhNnRNXNp4Jt80/custom/o;maxWidth=25;maxHeight=25" aria-label="20/06/2019 04:48 GMT+00:00; 04/07/2019 04:48 GMT+00:00; 7; 2; 8; 11 days 20 hours 45 minutes until due date" alt="20/06/2019 04:48 GMT+00:00; 04/07/2019 04:48 GMT+00:00; 7; 2; 8" tabindex="0"></div></td></tr><tr class=""><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/task/4574874" class="elements---global_a">Review Detailed Design</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">21/06/2019</p></td><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/page/home/record/lQBVF6bMMOMdjxUR6YqrUD8u9p8k-hS8unK9V9i437Bur4yN7zdzNRJSMWAdApXIql8ccBvoZkua3wEEaDBwWfrznT7V5_klxQu2CFX6LKxUlnS4dc/view/summary" class="elements---global_a">ROR000000001920</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">RD Demand Deployment Design</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">In Progress</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">EE ORD</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">05/07/2019</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">VIC-TAS</p></td><td class=""><div data-thumbnail="false" class="ImageGroup---image_gallery ImageGroup---default_direction ImageGroup---center"><img class="DocumentImage---image DocumentImage---icon" src="https://nbnco.appiancloud.com/suite/rest/a/content/latest/ioBVF6bMMOMdjxUR68quAKs_DZLlxmi4D-ulyeD5psPvbhNnRNXNp4Jt80/custom/o;maxWidth=25;maxHeight=25" aria-label="21/06/2019 04:38 GMT+00:00; 05/07/2019 04:38 GMT+00:00; 7; 2; 8; 12 days 20 hours 35 minutes until due date" alt="21/06/2019 04:38 GMT+00:00; 05/07/2019 04:38 GMT+00:00; 7; 2; 8" tabindex="0"></div></td></tr><tr class=""><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/task/4618136" class="elements---global_a">Review Detailed Design</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">21/06/2019</p></td><td class=""><div><p class="LinkGroup---link_group LinkGroup---align_start elements---global_p"><a href="https://nbnco.appiancloud.com/suite/sites/order-management/page/home/record/lQBVF6bMMOMdjxUR6YqrUD8u9p8k-hS8unK9V9i437Bur4yN7zdzNRJSMWAdApXIql8ccBvoZkua3wEEaPExWfrqRY8d7Vgkjhl0wj8YwGA6aNQ1m4/view/summary" class="elements---global_a">ROR000000002132</a></p></div></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">RD Demand Deployment Design</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">In Progress</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">EE ORD</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">05/07/2019</p></td><td class=""><p class="ParagraphText---richtext_paragraph ParagraphText---default_direction ParagraphText---align_start elements---global_p">VIC-TAS</p></td><td class=""><div data-thumbnail="false" class="ImageGroup---image_gallery ImageGroup---default_direction ImageGroup---center"><img class="DocumentImage---image DocumentImage---icon" src="https://nbnco.appiancloud.com/suite/rest/a/content/latest/ioBVF6bMMOMdjxUR68quAKs_DZLlxmi4D-ulyeD5psPvbhNnRNXNp4Jt80/custom/o;maxWidth=25;maxHeight=25" aria-label="21/06/2019 06:03 GMT+00:00; 05/07/2019 06:03 GMT+00:00; 7; 2; 8; 12 days 22 hours 0 minutes until due date" alt="21/06/2019 06:03 GMT+00:00; 05/07/2019 06:03 GMT+00:00; 7; 2; 8" tabindex="0"></div></td></tr></tbody></table></div></div></div></div>
答案 0 :(得分:1)
由于您知道元素的ID,因此最好使用:
reports = browser.find_element_by_id('NUMBER_headerCell_2')
reports.click()
看看是否可行!
答案 1 :(得分:1)
由于我无法发表评论,因此我必须在此处执行此操作。
想法-1选择父节点。例如,您尝试访问的 th 在一个类中。抓住该类元素grab_class= br.find_elements_by_class('class_name')
然后在其中找到grab_elements=grab_class.find_elements_by_(anything you feel like)
Idea-1抓住页面中的所有Thead / Th元素,然后手动访问列表。让我们假设您想要的元素是5th,然后选择lis [4]并对其进行操作。
只是可以做些什么的粗略草图。我希望它会有用。
NOTE
查找元素时要小心。 find_element
给出单个和第一个对象匹配条件,其中find_elemments
返回所有WebElement的列表。
答案 2 :(得分:1)
要处理,请使用WebdriverWait
和element_to_be_clickable
并遵循xpath来单击元素。
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@id='NUMBER_column2_header'][contains(.,'Reference ID')]"))).click()
您需要使用以下导入来执行以上代码。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
答案 3 :(得分:1)
要在元素上click()
,必须诱使 WebDriverWait 使元素可点击,并且可以使用以下任一解决方案:
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "th.PagingGridLayout---align_start[abbr='Reference ID'] > div.GridHeaderCell---grid_header_text"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//th[contains(@class, 'PagingGridLayout---align_start') and @abbr='Reference ID']/div[@class='GridHeaderCell---grid_header_text']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC