Finding an element and clicking on it to navigate to another webpage using selenium

Question:

I’m trying to use selenium to open up this webpage (https://app.powerbi.com/view?r=eyJrIjoiMzE2ZDIyN2YtODY1Yy00ZGY0LWE4YTktNDcxOTcwYWQyMjM5IiwidCI6IjcyMmVhMGJlLTNlMWMtNGIxMS1hZDZmLTk0MDFkNjg1NmUyNCJ9) and click on the Tram icon to navigate to the web page I want to scrape.

This is what I’ve tried up to now

from selenium import webdriver
driver=webdriver.Chrome()
driver.get("https://app.powerbi.com/view?r=eyJrIjoiMzE2ZDIyN2YtODY1Yy00ZGY0LWE4YTktNDcxOTcwYWQyMjM5IiwidCI6IjcyMmVhMGJlLTNlMWMtNGIxMS1hZDZmLTk0MDFkNjg1NmUyNCJ9")
driver.maximize_window()
x=driver.find_element("class name", "imageBackground")
print(x)

#driver.find_element("class name", "imageBackground").click()

However, the element returns none. I’m not sure how to find the element and click the div to navigate to the next page as well.

Asked By: z star

||

Answers:

I’ve updated your code.

I added a WebDriverWait that pauses until the tram element is clickable, then it clicks on the Tram element and goes to the next page.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

if __name__ == "__main__":
  driver=webdriver.Chrome()
  driver.get("https://app.powerbi.com/view?r=eyJrIjoiMzE2ZDIyN2YtODY1Yy00ZGY0LWE4YTktNDcxOTcwYWQyMjM5IiwidCI6IjcyMmVhMGJlLTNlMWMtNGIxMS1hZDZmLTk0MDFkNjg1NmUyNCJ9")
  driver.maximize_window()

  wait = WebDriverWait(driver, 30)
  tramElementXpath = '//*[@aria-label="Tram Bookmark . Click here to follow link"]'
  x = wait.until(EC.element_to_be_clickable((By.XPATH, tramElementXpath)))
  x.click()
Answered By: MrPeriodical
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.