0

I have tried to locate this element in the screenshot by searching for xpath, outerhtml, expanded xpath, css selector, class name, and others. I am automating a program to download a pdf from a website but for some reason the element that I need to click on to download it is a dynamic element. Here is my code (I have censored sensitive information):

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
username = "***"
password = '***'

driver = webdriver.Chrome()
def info_fill():
    driver.get ('***')
    driver.find_element_by_xpath('//*[@id="fsLoginUsernameField1249"]').send_keys(username)
    driver.find_element_by_xpath ('//*[@id="fsLoginPasswordField1249"]').send_keys(password)

def login():
    driver.find_element_by_xpath('//*[@id="fsEl_1249"]/div/div[1]/form/input[5]').click()

def portal():
    time.sleep(4)
    driver.get('***')

def get_grade_doc():
    time.sleep(4)
    driver.find_element_by_xpath('//*[@id="navigation-top"]/ic-sidebar/div/ic-tool-list/nav/ul/li[5]/a').click()
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "/html/body/iframe[@id='xh-bar']"))
        )
    finally:
        driver.find_element_by_xpath("/html/body/iframe[@id='xh-bar']").click()


info_fill()
login()
portal()
get_grade_doc()
driver.quit()

Here is a screenshot of the web inspector. The element that I am targeting is highlighted.

inspector.jpeg

this is the error message I am getting

<li _ngcontent-pts-c11="" class="divider__content documents__row clickable 
flex--space-between" role="link" tabindex="0"><div _ngcontent-pts-c11="" 
class="pr-4 float-left"><div _ngcontent-pts-c11="">Gradebook Detail - 
November 2020</div><!----><!----><!----><!----><div _ngcontent-pts-c11="" 
class="text-secondary pt-1">  </div><!----><div _ngcontent-pts-c11="" 
class="text-secondary pt-1"> 20-21 Upper<!----></div><!----><!----></div><div 
_ngcontent-pts-c11="" class="hide-tiny float-right"><!----><i _ngcontent-pts- 
c11="" class="fa fa-chevron-right fa-light float-right"></i></div><i 
_ngcontent- 
pts-c11="" class="fa fa-chevron-right fa-light float-right hide-until-tiny"> 
</i></li>
asfhas df
  • 11
  • 2
  • 1
    the problem is clearly with the path, did you generate it yoursel or used ide? it would be good if you could share the HTML or url ? – Northern Shadow Nov 20 '20 at 02:31

1 Answers1

0

Your element is in an iframe. This question has been answered many times before. Switch to an iframe through Selenium and python

# driver.switch_to.frame(‘frame_id’)
driver.switch_to.frame("xh-bar")

or this:

iframe = driver.find_element_by_xpath("//html/body/iframe[@id='xh-bar']")
driver.switch_to.frame(iframe)
DMart
  • 2,401
  • 1
  • 14
  • 19
  • Thanks. It is throwing an error saying it can't find that element when i run it as name = Dialogue Window. What should I change the name to? – asfhas df Nov 20 '20 at 03:31
  • sorry, copy paste error. I updated specifically for your example and locators – DMart Nov 20 '20 at 14:12
  • Still doesn't work. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/iframe[@id='xh-bar']"} – asfhas df Nov 20 '20 at 17:02
  • you may need to update the selector/xpath to fit your page. You did not provide an html snippet so I can't predict it absolutely. – DMart Nov 20 '20 at 17:37
  • When I add the xpath of that element, the code gives the same error. I edited my question to include the outer html. – asfhas df Nov 20 '20 at 20:51
  • what xpath did you try and what error did you get? Please update the question with the latest code. – DMart Nov 21 '20 at 02:27