使用selenium和python打开现有选项卡/窗口中新页面中的链接

时间:2017-08-10 06:42:19

标签: python selenium window-handles

我想输入搜索字词,然后转到下一页。在新页面中点击链接。如何使用selenium和python。我尝试使用下面给出的代码,但它给出了错误索引" ElementNotInteractableException"。我正在使用的代码是

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()

2 个答案:

答案 0 :(得分:0)

以下代码块将打开网址https://www.accessdata.fda.gov/scripts/cder/daf/,搜索lomitapide,展开手风琴Approval Date(s) and History, Letters, Labels, Reviews for NDA 203858,最后点击Review链接打开Drug Approval Package 1}}页面在下一个标签/页面中:

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()

答案 1 :(得分: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()