![](/img/trans.png)
[英]Using Selenium WebDriver in Python to Open Link in New Tab/Window
[英]Open a link in a new page opening in the existing tab/window using selenium and python
我要输入搜索词,然后转到下一页。在新页面中,单击链接。 如何使用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()
这是代码块,它将打开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()
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.