-1

I have the following element inside an iframe. This tag displays a ">" icon and will flip to the next graph on the URL when the user clicks on it. You can see it in the following URL where it says

< 1 of 16 >

https://msdh.ms.gov/msdhsite/_static/14,21995,420,873.html

<a>
<i class="glyphicon glyph-small pbi-glyph-chevronrightmedium middleIcon active pbi-focus-outline" focus-element="" tabindex="0" title="Next Page">
</i>
</a>

Using Selenium how can I send a click action to this element to flip to the next graph?

url='https://msdh.ms.gov/msdhsite/_static/14,21995,420,873.html'
p='my/path/to/chromedriver'
driver=webdriver.Chrome(p)
driver.get(url)
myframe=driver.find_element_by_class_name("flexibleFrame")
driver.switch_to.frame(myframe)
i = driver.find_element_by_class_name("glyphicon")

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
dmornad
  • 141
  • 1
  • 11
  • 1
    What have you tried? Did you try `i.click()`? I don't understand what the issue is. – JeffC Jun 24 '20 at 00:04
  • Thanks for your confirmation. My class selector was selecting the left Chevron so click didn't do anything as there is nothing to flip to. I fixed the selector and now its working as expected! – dmornad Jun 24 '20 at 06:13

1 Answers1

0

The > icon is within a <iframe> so to interact with the WebElement using Selenium you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.

  • Induce WebDriverWait for the desired visibility_of_element_located().

  • scrollIntoView() the WebElement.

  • You can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://msdh.ms.gov/msdhsite/_static/14,21995,420,873.html')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.flexibleFrame")))
    elem = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "i.glyphicon.glyph-small.pbi-glyph-chevronrightmedium.middleIcon.active.pbi-focus-outline[title='Next Page']")))
    driver.execute_script("arguments[0].scrollIntoView(true);", elem)
    elem.click()
    
  • Using XPATH:

    driver.get('https://msdh.ms.gov/msdhsite/_static/14,21995,420,873.html')
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='flexibleFrame']")))
    elem = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//i[@class='glyphicon glyph-small pbi-glyph-chevronrightmedium middleIcon active pbi-focus-outline' and @title='Next Page']")))
    driver.execute_script("arguments[0].scrollIntoView(true);", elem)
    elem.click()
    
  • Note : You have 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
    
  • Browser Snapshot:

msdhsite


Reference

You can find a relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352