2

I want to enter a search term and then move to next page.In the new page click on a link. How to do that using selenium and python.I tried using the code given below but it gives the error index "ElementNotInteractableException".The code I am using is

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

driver = webdriver.Firefox()
driver.get("https://www.accessdata.fda.gov/scripts/cder/daf/")

#Select element by id:
inputElement = driver.find_element_by_id("searchterm")

#Input search term=drugname
inputElement.send_keys('lomitapide')

#Now you can simulate hitting ENTER:
inputElement.send_keys(Keys.ENTER)

#wait until element located
download_link = WebDriverWait(driver,20).until(EC.presence_of_element_located((By.ID, "collapseApproval")))    
download_link.click()

#Click on Review to download the pdf
driver.find_element_by_link_text("Review").click()

browser.quit()
Mohua
  • 63
  • 2
  • 7
  • What are you trying to do here `window_1 = driver.window_handles[1]`? You have only searched for the text `lomitapide` and you are on the same tab/window, where did you find the `new window`? – undetected Selenium Aug 10 '17 at 13:38
  • Well initially I was trying to use find_element by id to click on a link "Approval,dates....." in the new page but it was showing the error "element not found" so I suspected that the error was because control was in the first page while I was trying to open the next page. – Mohua Aug 10 '17 at 14:38
  • Keep the question updated with your best code attempt as per your Question description along with the exact error and full error stack trace. – undetected Selenium Aug 10 '17 at 14:41
  • Thnk you very much will get back to you soon – Mohua Aug 10 '17 at 17:13
  • What are you trying to locate here `presence_of_element_located((By.ID, "collapseApproval")`? Thats a `div` and not clickable anyway. Can you sum up all of your exact manual steps please? – undetected Selenium Aug 11 '17 at 16:09
  • Th manual steps are: Go to https://www.accessdata.fda.gov/scripts/cder/daf/ page.Enter the search term eg "lomitapide" and Hit enter. In the new page Click on "Approval Date(s) and History, Letters, Labels, Reviews for NDA 203858" to expand the menu. Next click on the "Review" link under the "Original Approvals or Tentative Approvals" section. This will download my target PDF file. Hope this is what you wanted me to explain. If not please let me know. – Mohua Aug 13 '17 at 03:57
  • @DebanjanB if you have any solution please let me know... – Mohua Aug 14 '17 at 06:18

2 Answers2

0

Here is the code block which will open the URL https://www.accessdata.fda.gov/scripts/cder/daf/, search for lomitapide, expand the accordion Approval Date(s) and History, Letters, Labels, Reviews for NDA 203858 and finally click on Review link which opens the Drug Approval Package page in the next Tab/Page:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')

driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://www.accessdata.fda.gov/scripts/cder/daf/")

#Select element by id:
inputElement = driver.find_element_by_xpath("//input[@id='searchterm']")

#Input search term=drugname
inputElement.send_keys('lomitapide')

#Now you can simulate hitting ENTER:
inputElement.send_keys(Keys.ENTER)

# Scroll for the element to be within Viewport
driver.execute_script("window.scrollTo(0, 250);")

#wait until element located
download_link = WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='accordion']//a[@class='toggle collapsed'][starts-with(@title, 'Click to expand Approval')]")))
download_link.click()

#Click on Review which opens Drug Approval Package page in the next Tab/Page
driver.find_element_by_xpath("//table[@id='exampleApplOriG']//a[@title='Links to Review']").click()

driver.quit()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • the code seems to work fine but it downloads a a 'geckodriver.log 'file. While I wanted to download the review pdf file. @DebanjanB – Mohua Aug 14 '17 at 09:10
  • Well, `'geckodriver.log' file` will be always created as Python throws the logs generated at `stderr` in to `'geckodriver.log'`. Your final code line was at clicking on `Review` link which opens a new Tab/Window which I have mentioned in my Answer and addressed as well. – undetected Selenium Aug 14 '17 at 09:23
  • @ Debanjan yest its working now. :). Made some small changes to locate the files in my system.. Thank you :) – Mohua Aug 16 '17 at 06:01
  • When I am trying to download by clicking the "Medical Review(s)" link in the newly opened tab I am unable to download it. The code I am using is : – Mohua Sep 04 '17 at 11:53
0
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

binary = FirefoxBinary('/usr/bin/firefox')

driver = webdriver.Firefox(firefox_binary=binary, executable_path='/usr/bin/geckodriver')
driver.get("https://www.accessdata.fda.gov/scripts/cder/daf/")

#Select element by id:
inputElement = driver.find_element_by_xpath("//input[@id='searchterm']")

#Input search term=drugname
inputElement.send_keys('lomitapide')

#Now you can simulate hitting ENTER:
inputElement.send_keys(Keys.ENTER)

# Scroll for the element to be within Viewport
driver.execute_script("window.scrollTo(0, 250);")

#wait until element located
download_link = WebDriverWait(driver,15).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='accordion']//a[@class='toggle collapsed'][starts-with(@title, 'Click to expand Approval')]")))
download_link.click()

#Click on Review which opens Drug Approval Package page in the next Tab/Page
driver.find_element_by_xpath("//table[@id='exampleApplOriG']//a[@title='Links to Review']").click()

driver.implicitly_wait(10)



#Switch to new window
driver.switch_to_window("Drug Approval Package: Juxtapid (lomitapide) NDA 203858")

#Click on Medical Review which opens MedR
download_link = WebDriverWait(driver,15).until(EC.element_to_be_clickable(By.linkText("Medical Review(s)")))

download_link.click()




#driver.quit()
Mohua
  • 63
  • 2
  • 7