简体   繁体   English

单击 JavaScript 类型按钮“喜欢”使用 Selenium 和 Python

[英]Click on JavaScript Type Button "Like" Using Selenium and Python

I'm trying to click on like button.我正在尝试单击“赞”按钮。 I have tried "find_element_by_xpath()" but it didn't worked for me.我试过“find_element_by_xpath()”,但对我没有用。 Little confused how to do so有点迷茫怎么办

Here is the Inspects Complete Class Code:这是检查完整的 Class 代码:

    <div class="grid">
               <div class="user_hover_panel" style="width:100%;">
                  <ul class="list--horizontal float--left post-icons-list ng-isolate-scope" wt-on-outside-click="$ctrl.emojiOutSideClick($ctrl.postData.id)" wt-on-outside-click-enabled="$ctrl.toggleEmoji">
                     <li ng-class="{'selected': $ctrl.postData.is_liked, 'liked-desk' : true ,
                        'like-border-right' : ($ctrl.postData.privacyType == 'Personal' || $ctrl.postData.privacyType == 'Pro' || $ctrl.postData.privacyType == 'All' || $ctrl.postData.privacyType == 'Public'),
                        'like-border-none' : ($ctrl.postData.privacyType == 'Groups' || $ctrl.postData.privacyType == 'Staff' || $ctrl.postData.privacyType == 'Private')}" class="liked-desk like-border-right">
                        <a ng-mouseover="$ctrl.userContext.id != undefined &amp;&amp; !$ctrl.isMobile &amp;&amp; $ctrl.showLikeEmoji($ctrl.postData.id)" ng-mouseleave="$ctrl.hideLikeEmoji()" ng-click="!$ctrl.isMobile ? $ctrl.likeMedia($ctrl.postData.id, $root.context.id, $ctrl.postData.mediaTypeTagging, $ctrl.postData, true):$ctrl.toggleReaction($ctrl.postData.id)" class="cursor--pointer " data-own-entry="false">

           <!-- ngIf: !$ctrl.postData.is_liked -->
   <i ng-if="!$ctrl.postData.is_liked" class="icon icon-post-like private-link ng-scope">Like</i>
           <!-- end ngIf: !$ctrl.postData.is_liked -->

                           <!-- ngIf: $ctrl.postData.is_liked -->
                        </a>
                     </li>
                     <li class="comment-desk" ng-click="$ctrl.togglePostCommentBox()">
                        <a href="javascript:" data-own-entry="false">
                           <i class="icon icon-post-cmt private-link">Comment</i>
                        </a>
                     </li>
                     <!-- ngIf: !$ctrl.isModalView && !$ctrl.isPostAsComment --><li ng-if="!$ctrl.isModalView &amp;&amp; !$ctrl.isPostAsComment" ng-class="{
                     'shared-desk' : true}" ng-hide="$ctrl.postData.privacyType != 'Public'" class="ng-scope shared-desk">
                        <!-- ngIf: $ctrl.postData.mediaTypeTagging != 'photos' && $ctrl.checkBannerOrProfile(false) --><a data-ng-if="$ctrl.postData.mediaTypeTagging != 'photos' &amp;&amp; $ctrl.checkBannerOrProfile(false)" href="javascript:" ng-click="$ctrl.shareMedia('news', $ctrl.postData.post_share_id ? $ctrl.postData.post_share_id : $ctrl.postData.post_id, $ctrl.postData.privacyType)" class="" data-own-entry="false">
                        <i class="icon icon-post-share private-link">Share</i>
                        </a><!-- end ngIf: $ctrl.postData.mediaTypeTagging != 'photos' && $ctrl.checkBannerOrProfile(false) -->
                        <!-- ngIf: $ctrl.postData.mediaTypeTagging !='news' && $ctrl.postData.mediaTypeTagging == 'photos' && $ctrl.postData.id != undefined && $ctrl.checkBannerOrProfile(false) -->
                     </li><!-- end ngIf: !$ctrl.isModalView && !$ctrl.isPostAsComment -->
                  </ul>
                  <!-- ngIf: $ctrl.userContext.id != undefined  && $ctrl.toggleEmoji && $ctrl.postEmojiId == $ctrl.postData.id -->
               </div>
            </div>

I want to Click on:我想点击:

         <!-- ngIf: !$ctrl.postData.is_liked -->
<i ng-if="!$ctrl.postData.is_liked" class="icon icon-post-like private-link ng-scope">Like</i><!-- end ngIf: !$ctrl.postData.is_liked -->
           <!-- ngIf: $ctrl.postData.is_liked -->

Also I'm not sure if I'm clicking on right element.另外我不确定我是否点击了正确的元素。 I just want to click on like button.我只想点击喜欢按钮。 I'm new with python and selenium and do not know how to do this.我是 python 和 selenium 的新手,不知道该怎么做。

Here is my code what I'm doing !这是我正在做的代码!

from selenium import webdriver
import pyautogui as py

import time

PATH = "C:\Program Files (x86)\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.webtalk.co/")
time.sleep(3)
driver.maximize_window()

like_button = driver.find_element_by_xpath("//*[@id="30247788"]/div[5]/div/div/ul/li[1]/a/i")
like_button.click()

I got this error !我收到了这个错误!

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='30247788']/div[5]/div/div/ul/li[1]/a/i"}
  (Session info: chrome=90.0.4430.212)

The most sustainable XPath for the element would be something like this:该元素最可持续的 XPath 将是这样的:

//i[contains(text(),'Like')]

This XPath selects all the i tags containing the word Like .这个 XPath 选择所有i包含单词Like的标签。 You could also complicate it a bit (for eg add class selectors) if you are not sure this XPath would return only one element如果您不确定此 XPath 是否会仅返回一个元素,您也可以使其复杂一点(例如添加 class 选择器)

Tip : try to select elements on the website using your browser first, rather than directly through selenium.提示:尝试首先使用浏览器在网站上查看 select 元素,而不是直接通过 selenium。 See this看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM