0

I try to interact with the elements (button at this scenario) inside Disqus iframe on this webpage:

This is my trial code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time


path_to_chromedriver = r"c:\users\tv21\source\repos\chromedriver.exe"

driver = webdriver.Chrome(executable_path=path_to_chromedriver)

driver.maximize_window()

url = "https://www.postoj.sk/91472/po-navsteve-kina-si-precitajte-aj-kniznu-predlohu"

driver.get(url)

time.sleep(5)
   
button_to_close = driver.execute_script("return document.querySelector('body').querySelector('div.grv-dialog-host').shadowRoot.querySelector('div').querySelector('div.buttons-wrapper').querySelector('button.sub-dialog-btn.block_btn')")

ac = ActionChains(driver)
 
ac.move_to_element(button_to_close).click().perform()

open_discussion = driver.find_element_by_class_name('article-disqus-wrapper')

driver.execute_script("arguments[0].setAttribute('style','display: block;')", open_discussion)

disqus_thread = driver.find_element_by_id("disqus_thread")

iframe_element = disqus_thread.find_element_by_tag_name("iframe")

driver.switch_to.frame(iframe_element)

time.sleep(1)

button_to_load_more = driver.find_element_by_partial_link_text("Nahraj viac komentárov")

ac = ActionChains(driver)
     
ac.move_to_element(button_to_load_more).click().perform()

The issue is the last command:

ac.move_to_element(button_to_load_more).click().perform()

which shows an error: "move target out of bounds"

I tried instead:

button_to_load_more.click()

and

driver.execute_script("arguments[0].click();", button_to_load_more)

which both work completely fine as the alternatives and I can click the button.

However, I try to understand the reason for being out of bounds when using move_to_element(). I get exactly the same error always when I want to hover over any elements inside Disqus iframe too.

Can anyone help me to fix it or explain to me how to fix it?

lukuae
  • 1
  • 1

1 Answers1

0

First one dint worked because of the known issue in selenium,i guess you are using 3.4 hence facing this.(But it should work after trying newer version of selenium)

Some of the useful links fyr

Selenium MoveTargetOutOfBoundsException even after scrolling to element

https://github.com/SeleniumHQ/selenium/issues/4148

mk7644
  • 33
  • 2
  • 9
  • I upgraded Selenium to 4.1.0 and Chromedriver to 97.0.4692.71. It did not help :/ – lukuae Jan 26 '22 at 13:43
  • Okay, Well i did researched the same and below are my findings moveToElement don't scroll to the element if it is not visible on screen. It only controls the mouse and put the cursor on the element. If you want to scroll to an invisible element, you'll have to tell selenium to scroll the page until the element position is visible on the screen (you can compare element location with viewport) So, I used either ....................button_to_load_more.click(); ***or**** js.executeScript("arguments[0].click();", button_to_load_more); - it did worked.(I tested in Java:)) – mk7644 Jan 26 '22 at 15:28
  • I tried even extra commands driver.execute_script("arguments[0].scrollIntoView(true);", button_to_load_more) time.sleep(5) before calling ac.move_to_element(button_to_load_more).click().perform() So, button is visible in the screen. I literally see it. – lukuae Jan 27 '22 at 10:35