Element is not getting selected in Selenium Python Firefox Web Scraping

Question:

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>

Asked By: Sidda

||

Answers:

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
Answered By: undetected Selenium