![](/img/trans.png)
[英]How to fix Selenium Webdriver not opening a new tab on Firefox 68.0 and above?
[英]How to disable opening the page in a new tab in Selenium Webdriver in Python?
我正在抓網站: http : //www.delhidistrictcourts.nic.in/DLCIS-2016-2.html
此頁面中有許多鏈接。 用戶可以點擊任何這些鏈接(通過selenium web驅動程序)。 問題是,當用戶點擊這些鏈接時,它會在新標簽頁中打開,因為所有鏈接都有一個屬性(“ _target=blank
”)
知道如何強制,鏈接在同一個標簽中打開?
這是我寫的代碼
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
url = 'http://www.delhicourts.nic.in/DLCIS-2016-2.html'
driver=webdriver.Chrome()
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
driver.get(url)
try:
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "submit1"))).click()
except Exception as e:
print str(e)
您可以嘗試更新@target
的值:
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
driver.execute_script("arguments[0].target='_self';", link)
link.click()
要對頁面上的所有鏈接應用相同的內容:
links = wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, "a")))
for link in links:
driver.execute_script("arguments[0].target='_self';", link)
或者提取鏈接的@href
並get()
它:
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "CASE NUMBER WISE")))
url = link.get_attribute('href')
driver.get(url)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.