在 Selenium Python Firefox 网页抓取中未选择元素
我对下面代码片段中的列表项之一的元素选择复选框有问题。
我可以选择第 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是:
您可以添加 tag_name 和 class 属性,如下所示:
精确:
注意:您必须添加以下导入:
理想情况下,要调用您需要引发 WebDriverWait 的元素,element_to_be_clickable() 并且您可以使用以下任一定位器策略:
注意:您必须添加以下导入:
Instead of:
you can get more canonical adding the tag_name and the class attribute as follows:
precisely:
Note : You have to add the following imports :
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:
Note : You have to add the following imports :