![](/img/trans.png)
[英]How to click on navigation bar using selenium web driver with python?
[英]how to click on an image using selenium web driver and python as scripting language
我正在使用python編寫Selenium Webdriver程序。
我的問題是我無法使用x路徑或css路徑或ID單擊圖像。
driver.find_element_by_xpath("/div/svg/g/g[3]/g/rect").click()
driver.find_element_by_css_selector(""div#servicecatalogdisplaygraph.x-component svg g g g rect")
我的html頁面信息是:
<div id="servicecatalogdisplaygraph" class="x-component graph x-border-item x-box-item x- component-default" style="background: none repeat scroll 0% 0% white; padding: 10px; overflow: auto; margin: 0px; width: 484px; left: 0px; top: 0px; height: 491px;">
<svg style="width: 100%; height: 100%; min-width: 89px; min-height: 422px;">
<g>
<g>
<g>
<g>
<g style="cursor: pointer;">
<image xlink:href="/images/mxgraph/link.png" style="pointer-events:none" x="42" y="202" width="16" height="16" stroke-width="1" transform=" scale(1 1) translate(0 0)">
<rect stroke="none" fill="none" visibility="hidden" pointer-events="fill" x="42" y="202" width="16" height="16" stroke-width="1">
</g>
</g>
第一個技巧:盡可能使用較短的選擇器,您不需要這樣做。 讓我們使用代碼示例(通過您的dom假設元素是后代 ),找到並單擊圖像:
driver.find_element_by_css_selector("#servicecatalogdisplaygraph image").click()
要么
driver.find_element_by_css_selector("#servicecatalogdisplaygraph rect").click()
那應該工作。
PS確保圖像元素(或矩形)可見。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.