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

问题描述:

我想输入搜索词,然后移至下一页。在新页面中单击链接。如何做到这一点使用硒和python.I使用下面给出的代码试过,但它给错误索引“ElementNotInteractableException”我使用.The代码在使用硒和python的现有选项卡/窗口中打开的新页面中打开链接

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

什么是你想在这里'window_1 = driver.window_handles [做1]'?您只搜索了文字'lomitapide',并且您位于同一个标签页/窗口中,您在哪里找到了“新窗口”? – DebanjanB

+0

那么最初我试图通过ID使用find_element点击一个链接“审批,日期.....”在新的页面,但它显示错误“元素未找到”,所以我怀疑是因为控制错误在我试图打开下一页的时候出现在第一页。 – Mohua

+0

根据您的问题描述,随着最佳代码尝试更新问题以及确切的错误和完整的错误堆栈跟踪。 – DebanjanB

这里是代码块,这将打开URL https://www.accessdata.fda.gov/scripts/cder/daf/ ,搜索lomitapide,扩大手风琴Approval Date(s) and History, Letters, Labels, Reviews for NDA 203858终于在Review链接点击这将打开Drug Approval Package页面在接下来的选项卡/页:

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

该代码似乎工作正常,但它下载一个'geckodriver.log'文件。虽然我想下载revi ew pdf文件。 @DebanjanB – Mohua

+0

那么,''geckodriver.log'文件'将始终创建,因为Python会将'stderr'生成的日志放入''geckodriver.log'中。你的最终代码行是点击“评论”链接,该链接打开了一个新的选项卡/窗口,我在答复中已经提到并解决了这个问题。 – DebanjanB

+0

@ Debanjan现在正在工作。 :)。对我的系统中的文件进行了一些小的更改。谢谢:) – Mohua

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