2 回答
![?](http://img1.sycdn.imooc.com/5333a2320001acdd02000200-100-100.jpg)
TA贡献1848条经验 获得超2个赞
下面是如何测试的示例。测试 HTML:
<!DOCTYPE html>
<html>
<body>
<input type="text" value="my@email.com" id="mm">
<button onclick="myFunction()">Copy text</button>
<script>
function myFunction() {
var copyText = document.getElementById("mm");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
window.open();
}
</script>
</body>
</html>
测试代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
driver.get("file:///Users/***/Desktop/test.html")
# store input value
email = wait.until(ec.visibility_of_element_located((By.TAG_NAME, "input"))).get_attribute("value")
# click on button, that will copy value and open new tab
driver.find_element_by_tag_name("button").click()
# wait for the second window and switch to
wait.until(ec.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[-1])
# open google.com to check copied text
driver.get("https://www.google.com/")
google_q = driver.find_element_by_name("q")
# paste text to the google search input, SHIFT and INSERT keys for MacOS
google_q.send_keys(Keys.SHIFT, Keys.INSERT)
# assert copied value with stored
assert google_q.get_attribute("value") == email
# close current window and switch back to the first one
driver.close()
driver.switch_to.window(driver.window_handles[0])
![?](http://img1.sycdn.imooc.com/545861e40001199702200220-100-100.jpg)
TA贡献1845条经验 获得超8个赞
将剪贴板内容存储到一个变量中,并可以像往常一样对其进行断言。请尝试下面的代码,让我知道这是否有帮助。
Python 示例
import xerox
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
driver.implicitly_wait(15)
driver.get("https://clipboardjs.com/")
driver.find_element_by_xpath("//img[@alt='Copy to clipboard']").click() #clip board content copied here
i = xerox.paste() #clip board content stored into variable i
print i
print i == "npm install clipboard --save" #compare the clip board content against the expected value
driver.quit()
输出:
npm install clipboard --save
True
添加回答
举报