0

For reference visit https://rabirius.me/2020/02/14/bird-watching/ (not my website)

You may see a Like button there near a reblog button

I want python to click that but I get an error stating

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/div[2]/a"}

The code i wrote was

for posts in open_links:
       bot.get(posts)
       sleep(4)
       bot.find_element_by_xpath('/html/body/div/div/div[2]/a').click()
       sleep(2)

The HTML of the like button is

<div class="wpl-button like">
            <a href="#" title="177 bloggers like this." class="like sd-button" rel="nofollow">
                <span>Like</span>
            </a>
        </div>

Any Help would be Appreciated

3
  • Are you sure you have the right xPath? I think it might be /html/body/div/div/div[1]/a Commented Apr 8, 2020 at 4:49
  • check with driver.find_element_by_css_selector("a.like.sd-button").click() Commented Apr 8, 2020 at 4:50
  • This should help: How do I select elements inside an iframe with Xpath? Commented Apr 8, 2020 at 5:11

1 Answer 1

1

An iframe is present on the page, so you need to first switch to that iframe and then operate on the element and its recommended to use explicit wait to wait for the element to be present on the page. You can do it like:

for posts in open_links:
   bot.get(posts)
   driver.switch_to.frame(bot.find_element_by_tag_name('iframe'))
   like_element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class,'wpl-likebox')]//span[text()='Like']")))
   like_element.click()

You need 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
Sign up to request clarification or add additional context in comments.

7 Comments

when I use your code, I get this message that WebDriverWait is not defined, Ec is not defined By is not defined, Kindly Help
@ArjunBasandrai you need to add the imports. I have added those in my ans, please pick the updated ans
which imports do I need to add Please explain, I am quite New to Python
@ArjunBasandrai i have updated my answer and added that in my answer, please check my updated ans
I tried to put it in a try or except statement inside the for loop to see what it shows There I am getting this error ``` Message: element click intercepted: Element <iframe class="post-likes-widget jetpack-likes-widget" name="like-post-frame-30202391-21519-5e8e8570d7c98" height="55px" width="100%" frameborder="0" src="//widgets.wp.com/likes/index.html... ```
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.