I have written a code that logins to a webpage and tries to download a file. but after login and before downloading it requires selecting a particular report "Pass Loan Market" through the radio button window, can anyone pls assist with what could be done here? I am thinking to do this by pressing the tab key twice and then clicking on the down arrow key and finally clicking on the download button but the code I have written is not working. can you please help?
from selenium import webdriver
from selenium.webdriver.common.action_chains import Actionchains
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
wait = WebDriverWait(driver, 20)
url = "https://www.osaka.com"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.NAME, "username"))).send_keys("*****")
wait.until(EC.element_to_be_clickable((By.NAME, "password"))).send_keys("******")
wait.until(EC.element_to_be_clickable((By.TAG_NAME, "button"))).click()
ActionChains(driver).send_keys(Keys.Tab*2).perform()
CodePudding user response:
This can be done with use of XPath.
You can locate the parent tr
element based on the text of child td
and then access the second child - the input
.
I hope you saw these elements are inside the iframe. You need to switch into it in order to access elements there and to switch back to default content when you want to access elements out of it.
The following code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 30)
url = "https://products.ihsmarkit.com/home/index.jsp#LOANS.DOWNLOAD.download"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[type='email']"))).send_keys("***")
wait.until(EC.element_to_be_clickable((By.ID, "submitBtn"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[type='password']"))).send_keys("***")
wait.until(EC.element_to_be_clickable((By.ID, "submit"))).click()
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "MFrame")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//tr[./td[text()='Past Loan Market']]//input"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='downloadButton']"))).click()