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 >
<div >
<fd-order-supplier-list-item _nghost-ydw-c198="">
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><!---->
<div _ngcontent-ydw-c198="" >H</div><!----></div>
<div _ngcontent-ydw-c198="" style="">Taj Hotels</div>
<div _ngcontent-ydw-c198="" ><a _ngcontent-ydw-c198=""
>Add</a></div>
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><span _ngcontent-ydw-c198="" >check</span>
</div>
</div><!----><a _ngcontent-ydw-c198=""
><span
_ngcontent-ydw-c198="" >visibility</span></a><!---->
<div _ngcontent-ydw-c198="" >IN</div>
</div>
</fd-order-supplier-list-item>
<fd-order-supplier-list-item _nghost-ydw-c198="">
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><!---->
<div _ngcontent-ydw-c198="" >I</div><!----></div>
<div _ngcontent-ydw-c198="" style="">Oberoi Hotels</div>
<div _ngcontent-ydw-c198="" ><a _ngcontent-ydw-c198=""
>Add</a></div>
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><span _ngcontent-ydw-c198="" >check</span>
</div>
</div><!----><a _ngcontent-ydw-c198=""
><span
_ngcontent-ydw-c198="" >visibility</span></a><!---->
<div _ngcontent-ydw-c198="" >IN</div>
</div>
</fd-order-supplier-list-item>
<fd-order-supplier-list-item _nghost-ydw-c198="">
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" >Windsor Hotels</div>
<div _ngcontent-ydw-c198="" ><a _ngcontent-ydw-c198=""
>Add</a></div>
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><span _ngcontent-ydw-c198="" >check</span>
</div>
</div><!----><a _ngcontent-ydw-c198=""
><span
_ngcontent-ydw-c198="" >visibility</span></a><!---->
<div _ngcontent-ydw-c198="" >IN</div>
</div>
</fd-order-supplier-list-item>
<fd-order-supplier-list-item _nghost-ydw-c198="">
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" >Sheraton India</div>
<div _ngcontent-ydw-c198="" ><a _ngcontent-ydw-c198=""
>Add</a></div>
<div _ngcontent-ydw-c198="" >
<div _ngcontent-ydw-c198="" ><span _ngcontent-ydw-c198="" >check</span>
</div>
</div><!----><a _ngcontent-ydw-c198=""
><span
_ngcontent-ydw-c198="" >visibility</span></a><!---->
<div _ngcontent-ydw-c198="" >IN</div>
</div>
</fd-order-supplier-list-item>
</div>
</fd-order-supplier-list>
</body>
</html>
CodePudding user response:
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