Trying to interact with HTML page elements, but none of them are found

Question:

I’m trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly. I also tried using Puppeteer, but encountered the same problem. When I do $(‘.selector’) for the button I’m trying to click in the page console, it returns an object x.fn.init [prevObject: x.fn.init(1)]. I don’t know how to use this to interact with the element itself, but I believe the issue revolves around this.

from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://senamhi.gob.bo/index.php/sysparametros')
open_window_elem = driver.find_element("xpath", '//*[@id="myModal"]/div/div/div')
action = ActionChains(driver)
action.move_by_offset(500, 200)    
action.click()
action.perform()
y_coordinate = open_window_elem.location["y"]
x_coordinate = open_window_elem.location["x"]
action = ActionChains(driver)
action.move_by_offset(x_coordinate, y_coordinate)
action.click()
action.perform()
sleep(20)
driver.find_element('xpath', '/html/body/app-root/app-public-template/app-public-base/app-public-stations-info-export/div/div/div/div[2]/button').click()

The part of clicking somewhere on the screen is working, as when I enter the page, there appears to be a pop-up that requires clicking somewhere to dismiss. Then, a screen with a button comes up that I’m not able to access.

Answers:

As you can see in HTML the element that you want to click is inside object element.

Before interacting with any element inside object or iframe you need to instruct selenium to switch to that object, interact with the element that you want to interact with, and then switch back to the default context.

driver.switch_to.frame(driver.find_element("xpath", "//object"))

driver.find_element('xpath', '/html/body/app-root/app-public-template/app-public-base/app-public-stations-info-export/div/div/div/div[2]/button').click()

driver.switch_to.default_content()
Answered By: puchal