Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用selenium在动态表中搜索字符串_Python_Selenium_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Python 使用selenium在动态表中搜索字符串

Python 使用selenium在动态表中搜索字符串,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,我有一个动态表,其中有一列/字段名为“reference id”。表是动态的,但列不是。我想将所有引用ID作为字符串抓取 下面是字符串“reference ID”所在单元格的一个片段。如果单击此单元格,它将根据单击的次数按升序或降序对动态表进行排序 编辑:已包含下面的整个表格标题 <th tabindex="0" scope="col" id="NUMBER_headerCell_2" aria-sort="none" class="PagingGridLayout---align_sta

我有一个动态表,其中有一列/字段名为“reference id”。表是动态的,但列不是。我想将所有引用ID作为字符串抓取

下面是字符串“reference 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>
但是,我得到一个错误:

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>


由于您知道元素的ID,因此最好使用:

reports = browser.find_element_by_id('NUMBER_headerCell_2')
reports.click()

看看这样行不行

既然您知道元素的ID,也许您最好使用:

reports = browser.find_element_by_id('NUMBER_headerCell_2')
reports.click()

看看这样行不行

因为我不能发表评论,所以我不得不在这里发表评论

Idea-1选择父节点。例如,您试图访问的th位于类内部。抓取该类元素抓取\u class=br。通过\u class'class\u name'查找\u元素 然后找到其中的元素,比如grab\u elements=grab\u class.find\u elements\u by\u任何你喜欢的东西

Idea-1抓取页面中的所有THAD/Th元素并手动访问列表。让我们假设您想要的元素是5th,然后选择lis[4]并对其进行操作

只是一个可以做什么的粗略草图。我希望它会有用


注意:查找元素时要小心。find_元素给出一个单独的和第一个对象匹配条件,其中find_元素返回所有webelement的列表

因为我不能发表评论,所以我不得不在这里发表评论

Idea-1选择父节点。例如,您试图访问的th位于类内部。抓取该类元素抓取\u class=br。通过\u class'class\u name'查找\u元素 然后找到其中的元素,比如grab\u elements=grab\u class.find\u elements\u by\u任何你喜欢的东西

Idea-1抓取页面中的所有THAD/Th元素并手动访问列表。让我们假设您想要的元素是5th,然后选择lis[4]并对其进行操作

只是一个可以做什么的粗略草图。我希望它会有用

注意:查找元素时要小心。find_元素给出一个单独的和第一个对象匹配条件,其中find_元素返回所有webelement的列表

要处理,请使用WebdriverWait和元素可单击,并使用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
要处理此问题,请使用WebdriverWait和元素可单击,并按照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
要单击该元素,您必须引导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:

使用CSS_选择器:

使用XPATH:

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
要单击该元素,您必须引导WebDriverWait使该元素可单击,并且您可以使用以下任一解决方案:

使用CSS_选择器:

使用XPATH:

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

我试过了,但是我得到了一个错误,错误是“NoTouchElementException:Message:没有这样的元素:找不到元素:”。。。我在那行之前放了一个time.sleep20,看看这是否是一个加载问题,但这并没有解决它,这很奇怪!你介意发布其余的HTML代码吗?如果它很大,那么至少是你试图点击的标签周围的部分,在文章底部的整个标题中使用html编辑我的作品:好的,为了测试起见,试着设置时间。sleep60而不是20,看看会发生什么。这不是一个好的做法,因为设置一个可能更好,但是让我们给页面一整分钟的时间来加载它。我们也尝试过了,仍然没有任何luckI尝试过,但是我得到一个错误,错误是“NoTouchElementException:Message:没有这样的元素:无法定位元素:”。。。我在那行之前放了一个time.sleep20,看看这是否是一个加载问题,但这并没有解决它,这很奇怪!你介意发布其余的HTML代码吗?如果它很大,那么至少是你试图点击的标签周围的部分,在文章底部的整个标题中使用html编辑我的作品:好的,为了测试起见,试着设置时间。sleep60而不是20,看看会发生什么。这不是一个好的实践,因为设置一个可能更好,但是让我们给页面一整分钟的时间来加载它。同样,仍然没有任何运气检查表是否在iframeHey@Mahesh内。这是一个非常好的解释!谢谢我实现了代码,现在我得到一个错误:AttributeError:'list'对象没有属性'find_elements_by_id'在对象上执行它。我不太明白你的意思sorry@says当您执行查找时
通过,它返回一个包含所有WebElement的列表。只需逐个选择类、标记和id,直到到达子节点。打个比方,这就像当你去国外朋友家时,你必须先问国家,然后问国家,然后再问国家,直到你到达某人的家。首先选择父类,然后找到所有thead,然后在其中找到你的元素。嘿@Mahesh这真是一个很棒的解释!谢谢我实现了代码,现在我得到一个错误:AttributeError:'list'对象没有属性'find_elements_by_id'在对象上执行它。我不太明白你的意思sorry@says执行find_elements_by时,它返回一个包含所有WebElements的列表。只需逐个选择类、标记和id,直到到达子节点。打个比方,这就像当你去国外朋友家时,你必须先问国家,然后问国家,然后再问国家,直到你到达某人的家。首先选择父类,然后找到所有thead,然后在其中找到你的元素。我有这些导入。我在顶部尝试了一行代码,但得到了一个超时,例外是我没有这些导入。我尝试了顶部的代码行,但出现了超时异常