在 Selenium Python Firefox 网页抓取中未选择元素

发布于 2025-01-10 09:15:07 字数 6566 浏览 0 评论 0原文

我对下面代码片段中的列表项之一的元素选择复选框有问题。

我可以选择第 1 个、第 2 个和第 1 个下面代码中的第四个元素,但是我无法选择第三个元素。

当我只给出泰姬陵酒店、欧贝罗伊酒店和印度喜来登酒店时。所有三个都通过 python selenium web scraping 进行检查。当我将温莎酒店添加到列表时,它没有选择它,脚本显示如下错误。

selenium.common.exceptions.ElementNotInteractableException:消息:元素无法滚动到视图

注意:每当选择一个项目时,它的类名就会更改为“list-item star ng-star-inserted active” “

我使用 Windows 10、Pycharm IDE 运行此程序。

硒==3.7.0 密码学=3.1.1 熊猫

Python脚本:

txt_button = browser.find_element_by_xpath('//*[text() = "' + element_to_be selected + '"]')
txt_button.click()

<!DOCTYPE html>
<html lang="en">

<body>
<fd-order-supplier-list _ngcontent-ydw-c199="">
    <div class="block-list block-list-selectable block-list-sm">
        <div class="the-list">
            <fd-order-supplier-list-item class="list-item star ng-star-inserted active" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">
                    <div _ngcontent-ydw-c198="" class="item-logo placeholder"><!---->
                        <div _ngcontent-ydw-c198="" class="logo-placeholder-inner ng-star-inserted">H</div><!----></div>
                    <div _ngcontent-ydw-c198="" class="item-name" style="">Taj Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted active" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">
                    <div _ngcontent-ydw-c198="" class="item-logo placeholder"><!---->
                        <div _ngcontent-ydw-c198="" class="logo-placeholder-inner ng-star-inserted">I</div><!----></div>
                    <div _ngcontent-ydw-c198="" class="item-name" style="">Oberoi Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">

                    <div _ngcontent-ydw-c198="" class="item-name">Windsor Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">

                    <div _ngcontent-ydw-c198="" class="item-name">Sheraton India</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
        </div>
</fd-order-supplier-list>
</body>
</html>

I have problem with element selection checkbox with one of the list item in below code snippet.

I'm able to select 1st 2nd & 4th Element in below code, however I'm not able to select 3rd element.

When I give only Taj Hotels, Oberoi Hotels and Sheraton India. All three are getting checked by python selenium web scrapping. When I add Windsor Hotels to list, it's not selecting it script shows error like below.

selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view

Note: whenever an item is selected it's class name is getting is changed to "list-item star ng-star-inserted active"

I'm running this using windows 10, Pycharm IDE.

selenium==3.7.0
cryptography=3.1.1
pandas

Python Script:

txt_button = browser.find_element_by_xpath('//*[text() = "' + element_to_be selected + '"]')
txt_button.click()

<!DOCTYPE html>
<html lang="en">

<body>
<fd-order-supplier-list _ngcontent-ydw-c199="">
    <div class="block-list block-list-selectable block-list-sm">
        <div class="the-list">
            <fd-order-supplier-list-item class="list-item star ng-star-inserted active" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">
                    <div _ngcontent-ydw-c198="" class="item-logo placeholder"><!---->
                        <div _ngcontent-ydw-c198="" class="logo-placeholder-inner ng-star-inserted">H</div><!----></div>
                    <div _ngcontent-ydw-c198="" class="item-name" style="">Taj Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted active" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">
                    <div _ngcontent-ydw-c198="" class="item-logo placeholder"><!---->
                        <div _ngcontent-ydw-c198="" class="logo-placeholder-inner ng-star-inserted">I</div><!----></div>
                    <div _ngcontent-ydw-c198="" class="item-name" style="">Oberoi Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">

                    <div _ngcontent-ydw-c198="" class="item-name">Windsor Hotels</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
            <fd-order-supplier-list-item class="list-item star ng-star-inserted" _nghost-ydw-c198="">
                <div _ngcontent-ydw-c198="" class="list-item-inner">

                    <div _ngcontent-ydw-c198="" class="item-name">Sheraton India</div>
                    <div _ngcontent-ydw-c198="" class="item-btn"><a _ngcontent-ydw-c198=""
                                                                    class="btn btn-outline-default btn-xs">Add</a></div>
                    <div _ngcontent-ydw-c198="" class="item-check ng-star-inserted">
                        <div _ngcontent-ydw-c198="" class="item-checkbox"><span _ngcontent-ydw-c198="" class="icon">check</span>
                        </div>
                    </div><!----><a _ngcontent-ydw-c198=""
                                    class="btn btn-sm btn-icon btn-text item-info-link ng-star-inserted"><span
                        _ngcontent-ydw-c198="" class="icon">visibility</span></a><!---->
                    <div _ngcontent-ydw-c198="" class="item-upper-text">IN</div>
                </div>
            </fd-order-supplier-list-item>
        </div>
</fd-order-supplier-list>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下客 2025-01-17 09:15:07

而不是:

txt_button = browser.find_element_by_xpath('//*[text() = "' + element_to_be selected + '"]')

您可以添加 tag_nameclass 属性,如下所示:

txt_button = browser.find_element_by_xpath("//div[@class='item-name' and text()='" + element_to_be selected + "']")

精确:

txt_button = driver.find_element(By.XPATH, "//div[@class='item-name' and text()='" + element_to_be selected + "']")

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

from selenium.webdriver.common.by import By

理想情况下,要调用您需要引发 WebDriverWait 的元素,element_to_be_clickable() 并且您可以使用以下任一定位器策略

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='item-name' and text()='" + element_to_be selected + "']"))).click()

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

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

Instead of:

txt_button = browser.find_element_by_xpath('//*[text() = "' + element_to_be selected + '"]')

you can get more canonical adding the tag_name and the class attribute as follows:

txt_button = browser.find_element_by_xpath("//div[@class='item-name' and text()='" + element_to_be selected + "']")

precisely:

txt_button = driver.find_element(By.XPATH, "//div[@class='item-name' and text()='" + element_to_be selected + "']")

Note : You have to add the following imports :

from selenium.webdriver.common.by import By

Ideally to invoke click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='item-name' and text()='" + element_to_be selected + "']"))).click()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文