Python 如何在bokeh文档中找到带有selenium webdriver的按钮?

Python 如何在bokeh文档中找到带有selenium webdriver的按钮?,python,selenium-webdriver,bokeh,Python,Selenium Webdriver,Bokeh,我正在使用python和bokeh创建绘图,并启用一些简单的用户交互(例如,使用按钮)。我希望使用bokeh-serve-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 cu

我正在使用python和bokeh创建绘图,并启用一些简单的用户交互(例如,使用按钮)。我希望使用
bokeh-serve-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>

Bokeh应用程序
设置日志级别(“信息”);
{}
(功能(){
var fn=函数(){
Bokeh.safety(函数(){
(函数(根){
函数嵌入文档(根){
var docs_json=document.getElementById('7f9e0a64-7083-4961-94b7-d5d415349743')。textContent;
var render_items=[{“elementid”:“29ecc46a-da2b-4518-a6c4-985fbd84dd10”,“sessionid”:“YMYTVDZ3FLXOOB7DEQZ3DED3JKE7ZQLJ9Z7XS9LXLVY”,“将_用于_标题:true}”;
root.Bokeh.embed.embed_项(docs_json,render_项);
}
if(root.Bokeh!==未定义){
嵌入文档(根);
}否则{
var=0;
var定时器=设置间隔(函数(根){
if(root.Bokeh!==未定义){
嵌入文档(根);
清除间隔(计时器);
}
尝试++;
如果(尝试次数>100){
log(“Bokeh:错误:由于缺少BokehJS库,无法运行BokehJS代码”)
清除间隔(计时器);
}
},10,根)
}
})(窗口);
});
};
如果(document.readyState!=“加载”)fn();
else文件。添加了EventListener(“DOMContentLoaded”,fn);
})();

您可以为小部件提供一个类:

bt = Button(label="click me", css_classes=["my_button"])
然后选择它:

driver.find_element_by_class_name("my_button")

它是按钮节点的父div节点。

我们不能为按钮对象分配id吗?在实例化按钮时,我尝试将id设置为关键字参数,但在浏览器上检查时,它似乎没有显示出来。