0

My code accesses a page, and I am trying to click on the button that says "Physician Program" on the menu list. If you click on this on the browser, it directs you to a new webpage.

However, there is no href on the html of the page that would help me find this link via code (I am assuming because it is JavaScript?) Currently, I just used its Xpath.

My question is - If I am able to click on it in a browser, shouldnt I be able to click on it using Selenium? If so, how can this be done?

import time
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()

driver.get('https://www.kidney.org/spring-clinical/program')
time.sleep(6)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
element1 = driver.find_element_by_xpath('//*[@id="dx-c7ad8807-6124-b55e-d292-29a4389dee8e"]/div')
element1.click()
2
  • If you can successfully use xpath to locate the button, then what is the problem? Commented Mar 11, 2021 at 23:08
  • also why is it a webelement instead of android or mobilelement Commented Mar 11, 2021 at 23:28

2 Answers 2

2

The element is inside iframe you need to switch to iframe

driver.switch_to.frame("SCM20 Advanced Practitioner Program")
element1 = driver.find_element_by_xpath("//div[text()='Physician Program']")
element1.click()

Ideally you should use webdriverwait and wait for frame to be available.

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"SCM20 Advanced Practitioner Program"))) 
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH "//div[text()='Physician Program']"))).click()

You need to import below libraries

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, what is iFrame and how did you find the "SCM20 Advanced Practitioner Program" is this within the inspect section of the HTML? Can i find the other options within here?
1
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

import subprocess
#other imports
import time
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()

driver.get('https://www.kidney.org/spring-clinical/program')
time.sleep(6)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
frame= WebDriverWait(driver,10).until(EC.presence_of_element_located(
    (By.NAME, "SCM20 Advanced Practitioner Program")))
driver.switch_to.frame(frame)
options = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located(
    (By.CSS_SELECTOR, '[class="track-selector-popup"] [role="option"]')))

options[0].click()

input()

Element is inside iframe so switch to it and also use waits, to switch back and interact with elements outside the frame use:

  driver.switch_to.default_content()

5 Comments

Thanks, what is iFrame and how did you find the "SCM20 Advanced Practitioner Program" is this within the inspect section of the HTML? Can i find the other options within here?
That is the name property of the element with tag name iframe , iframe creates a ln isolated frame inside your webpage
I am right clicking on the webpage and hitting inspect, and I cannot find what you found? I want to be able to do this to other options (pharmacist program etc)
Just use options[2].click and so on to choose respective option
Just scroll backward and see the parent of pharmacist program , you can see there is a element called iframe as a parent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.