2015-01-05 17 views

Trả lời

11

Sử dụng một ActionChain với key_down bấm phím điều khiển, và key_up để phát hành nó:

import time 
from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.keys import Keys 

driver = webdriver.Chrome() 

driver.get('http://google.com') 
element = driver.find_element_by_link_text('About') 

ActionChains(driver) \ 
    .key_down(Keys.CONTROL) \ 
    .click(element) \ 
    .key_up(Keys.CONTROL) \ 
    .perform() 

time.sleep(10) # Pause to allow you to inspect the browser. 

driver.quit() 
0

Dưới đây là những gì tôi đã thử cho Selenium WebDriver với Java ràng buộc và nó làm việc cho tôi. Nếu bạn muốn mở Liên kết trong tab mới theo cách thủ công, bạn có thể thực hiện điều này bằng cách thực hiện theo ngữ cảnh Nhấp vào liên kết và chọn tùy chọn 'Mở tab mới'. Dưới đây là việc thực hiện trong trình điều khiển web Selenium với Java ràng buộc.

Actions newTab= new Actions(driver); 
WebElement link = driver.findElement(By.xpath("//xpath of the element")); 

//Open the link in new window 
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform(); 

Trình điều khiển web xử lý tab mới theo cùng cách với cửa sổ mới. Bạn sẽ phải chuyển sang tab mới mở bằng tên cửa sổ của nó.

driver.switchTo().window(windowName); 

Bạn có thể theo dõi tên cửa sổ để giúp bạn dễ dàng điều hướng giữa các tab.

6

Hai giải pháp khả thi:

mở một tab mới

self.driver = webdriver.Firefox() 
self.driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') 

này là giải pháp cho MAC OSX. Trong những trường hợp khác mà bạn có thể sử dụng các tiêu chuẩn Keys.CONTROL + 't'

mở một webdriver mới

driver = webdriver.Firefox() #1st window 
second_driver = webdriver.Firefox() #2nd windows 
-2

Sau đây là làm việc cho tôi để mở liên kết trong tab mới:

String link = Keys.chord(Keys.CONTROL,Keys.ENTER); 
    driver.findElement(By.linkText("yourlinktext")).sendKeys(link); 

Mã trên nằm trong java. bạn có thể chuyển đổi sang python một cách dễ dàng.

Vui lòng hỏi xem có bất kỳ truy vấn nào không.

Các vấn đề liên quan