【问题标题】:Open a link in a new page opening in the existing tab/window using selenium and python使用 selenium 和 python 在现有选项卡/窗口中打开的新页面中打开链接
【发布时间】:2017-08-10 06:42:18
【问题描述】:

我想输入一个搜索词然后转到下一页。在新页面中单击一个链接。如何使用 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()

【问题讨论】:

  • 你想在这里做什么window_1 = driver.window_handles[1]?您只搜索了文本lomitapide,并且您在同一个选项卡/窗口中,您在哪里找到new window
  • 最初我试图通过 id 使用 find_element 单击新页面中的链接“批准,日期.....”,但它显示错误“未找到元素”所以我怀疑这个错误是因为当我试图打开下一页时控制在第一页。
  • 根据您的问题描述以及确切的错误和完整的错误堆栈跟踪,使用您的最佳代码尝试更新问题。
  • 非常感谢您会尽快回复您
  • 你想在这里找到什么presence_of_element_located((By.ID, "collapseApproval")?那是div,无论如何都不能点击。你能总结一下你所有的具体手动步骤吗?

标签: python selenium window-handles


【解决方案1】:

这是打开 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()

【讨论】:

  • 代码似乎工作正常,但它下载了一个“geckodriver.log”文件。虽然我想下载评论 pdf 文件。 @DebanjanB
  • 好吧,'geckodriver.log' file 将始终被创建,因为 Python 将在 stderr 生成的日志抛出到 'geckodriver.log'。您的最终代码行是单击Review 链接,该链接会打开一个新的选项卡/窗口,我在答案中已经提到并解决了这个问题。
  • @Debanjan 现在正在工作。 :)。做了一些小的改动来定位我系统中的文件。谢谢:)
  • 当我尝试通过单击新打开的选项卡中的“医学评论”链接进行下载时,我无法下载它。我使用的代码是:
【解决方案2】:
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2013-03-22
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    相关资源
    最近更新 更多