-1

I'm trying to click on like button. I have tried "find_element_by_xpath()" but it didn't worked for me. Little confused how to do so

Here is the Inspects Complete Class Code:

    <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.

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)
Akif Ejaz
  • 1
  • 1
  • Where is your code? – Prophet May 30 '21 at 11:13
  • Just Added, Would be really helpful if you can review again : – Akif Ejaz May 30 '21 at 11:31
  • Well, now it's clearer. What element are you trying to locate with that strange XPath `//*[@id="30247788"]/div[5]/div/div/ul/li[1]/a/i`? – Prophet May 30 '21 at 11:36
  • actually the path "//*[@id="30247788"]/div[5]/div/div/ul/li[1]/a/i" is when i clicked on inspect and copied "Xpath" of the element. ``` Like ``` This is the element , I clicked on it and copied that Xpath. – Akif Ejaz May 30 '21 at 19:56
  • Exactly after opening the home page - `driver.get("https://www.webtalk.co/")` - i see no like button and no element matching the Xpath you provided – Prophet May 30 '21 at 20:32
  • Would you mind sparing a sec, if I provide a login pass, Please Visit (https://www.webtalk.co/o/home) and Login With Email : " jikepa1391@slowimo.com " and Pass : " @Aasdfg1234 " When you logged in you will see, in "(https://www.webtalk.co/feed) " Some posts just like Facebook having "Like Comment & Share Button " Let me know if you figure out this – Akif Ejaz Jun 01 '21 at 08:20
  • I see the feed with those options. What is the question? – Prophet Jun 01 '21 at 08:25
  • I want to click on like buttons on the post available on site I shared. – Akif Ejaz Jun 01 '21 at 12:44

1 Answers1

0

The most sustainable XPath for the element would be something like this:

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

This XPath selects all the i tags containing the word Like. You could also complicate it a bit (for e.g. add class selectors) if you are not sure this XPath would return only one element

Tip: try to select elements on the website using your browser first, rather than directly through selenium. See this

Just Mohit
  • 141
  • 1
  • 13