我正在使用 python 和 bokeh 来创建绘图并启用一些简单的用户交互(例如使用按钮)。我想使用 bokehserve myapp.py
运行 python 脚本,并使用 pytest 和 selenium webdriver 与浏览器中的 bokeh 文档交互并测试预期的工作流程。
鉴于此应用程序代码:
# myapp.py
# run this with: bokeh serve myapp.py
from bokeh.models import Button
from bokeh.io import curdoc
# add a button widget
button = Button(label="Press Me")
# add to the document
curdoc().add_root(button)
以及这个(失败的)测试代码
from selenium import webdriver
# Create a new instance of the Chrome driver
driver = webdriver.Chrome()
# get the served bokeh document
driver.get("http://localhost:5006/myapp")
# find the element, variant 1, this gives a
#
# selenium.common.exceptions.NoSuchElementException:
# Message: no such element: Unable to locate element:
# {"method":"name","selector":"button"}
inputElement = driver.find_element_by_name("button")
print(inputElement)
# find the element, variant 2, this gives a
#
# selenium.common.exceptions.InvalidSelectorException:
# Message: invalid selector: Compound class names not permitted
# inputElement = driver.find_element_by_class_name("bk-widget bk-layout-fixed")
如何使用 selenium webdriver 查找/访问按钮?
这是由 bokeh 创建/提供的 html 页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Application</title>
<link rel="stylesheet" href="static/css/bokeh.min.css?v=f7fe43f4980a82921415acbd60d5fd66" type="text/css" />
<link rel="stylesheet" href="static/css/bokeh-widgets.min.css?v=e2198c0464ced10f820b120714112595" type="text/css" />
<link rel="stylesheet" href="static/css/bokeh-tables.min.css?v=89abce161b192f3f74677f425212bd62" type="text/css" />
<script type="text/javascript" src="static/js/bokeh.min.js?v=f8e9c08dc7d95ab8b8ffd3109ad0ae0d"></script>
<script type="text/javascript" src="static/js/bokeh-widgets.min.js?v=a2dd57b513ce6b5a818fc187fe161f28"></script>
<script type="text/javascript" src="static/js/bokeh-tables.min.js?v=290ceee4270c39d8e53f1ffcc71980c6"></script>
<script type="text/javascript" src="static/js/bokeh-gl.min.js?v=7301df140b8889d43d68cef71bd7a987"></script>
<script type="text/javascript">
Bokeh.set_log_level("info");
</script>
</head>
<body>
<div class="bk-root">
<div class="bk-plotdiv" id="29ecc46a-da2b-4518-a6c4-985fbd84dd10"></div>
</div>
<script type="application/json" id="7f9e0a64-7083-4961-94b7-d5d415349743">
{}
</script>
<script type="text/javascript">
(function() {
var fn = function() {
Bokeh.safely(function() {
(function(root) {
function embed_document(root) {
var docs_json = document.getElementById('7f9e0a64-7083-4961-94b7-d5d415349743').textContent;
var render_items = [{"elementid":"29ecc46a-da2b-4518-a6c4-985fbd84dd10","sessionid":"YMyTvdz3FlXoOoB7DEqz3dED3jKe7zqLj9z7xs9lxlvY","use_for_title":true}];
root.Bokeh.embed.embed_items(docs_json, render_items);
}
if (root.Bokeh !== undefined) {
embed_document(root);
} else {
var attempts = 0;
var timer = setInterval(function(root) {
if (root.Bokeh !== undefined) {
embed_document(root);
clearInterval(timer);
}
attempts++;
if (attempts > 100) {
console.log("Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing")
clearInterval(timer);
}
}, 10, root)
}
})(window);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
</body>
</html>
最佳答案
您可以为小部件指定一个类:
bt = Button(label="click me", css_classes=["my_button"])
然后选择它:
driver.find_element_by_class_name("my_button")
它是按钮节点的父div节点。
关于python - 如何在 Bokeh 文档中找到带有 selenium webdriver 的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49823206/