我正在使用带有firefox驱动程序的selenium web驱动程序。我必须检查这个元素是否在页面中使用它的XPath,所以我可以继续下一步但我不知道该怎么做。
//*[@id='view_container']/form/div[2]/div/div/div/ul/li[1]/div/div[2]
if (idk what i have to write here)
{
driver.FindElement(By.XPath("//[@id='view_container']/form/div[2]/div/div/div/ul/li[1]/div/div[2]"));
// if we found xpath
}
else
{
// if don't find xpath
}
答案 0 :(得分:0)
您可以使用@font-face{
font-family:'widget_easy_sidebar_menu_widget';
src:url("../fonts/fontello.eot?35265427");
src:url("../fonts/fontello.eot?35265427#iefix") format("embedded-opentype"),url("../fonts/fontello.woff2?35265427") format("woff2"),url("../fonts/fontello.woff?35265427") format("woff"),url("../fonts/fontello.ttf?35265427") format("truetype"),url("../fonts/fontello.svg?35265427#fontello") format("svg");
font-weight:normal;font-style:normal
}
.widget_easy_sidebar_menu_widget ul{
list-style-type:none
}
.widget_easy_sidebar_menu_widget .current-menu-item>span>a,.widget_easy_sidebar_menu_widget .current-menu-ancestor>span>a{
font-weight:700
}
.widget_easy_sidebar_menu_widget li.menu-item{
position:relative;
margin-top:0px;
margin-bottom:0px;
padding:0px;
border-bottom:0px solid #eee;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box
}
.widget_easy_sidebar_menu_widget li.menu-item .link__wrap{
position:relative;
display:block
}
.widget_easy_sidebar_menu_widget li.menu-item a{
display:block;
padding:8px 0px;
width:100%;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
box-sizing:border-box;
margin-left: -30px;
}
.widget_easy_sidebar_menu_widget li.menu-item a .nav_desc{
display:block;
color:#777
}
.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler{
position:absolute;
padding:0px;
margin:0px;
top:2px;
right:0px;
width:34px;
height:34px;
border:1px solid transparent;
text-align:center;
outline:none;
font-size:12px;
}
.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler i{
font-family:"widget_easy_sidebar_menu_widget";
font-style:normal;
font-weight:normal;
speak:none;color:#555;
text-decoration:inherit;
width:100%;
height:100%;
line-height:34px;
display:block;
-webkit-font-smoothing:antialiased;
-moz-osx-font-smoothing:grayscale
}
.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler i:before{
content:'\e800'
}
.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler.toggle__open i:before{
content:'\e801'
}
.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler,.widget_easy_sidebar_menu_widget li.menu-item .easy-sidebar-menu-widget-toggler:focus{
background:rgba(239,239,239,0.4);
border:1px solid #eee
}
.widget_easy_sidebar_menu_widget li.menu-item.menu-item-has-children .easy-sidebar-menu-widget-link{
padding-right:40px
}
.widget_easy_sidebar_menu_widget li.menu-item .sub-menu{
display:none;
position:relative;
margin-left: -25px
}
.widget_easy_sidebar_menu_widget li.menu-item .sub-menu li:first-child{
border-top:0px solid #eee
}
.widget_easy_sidebar_menu_widget li.menu-item .sub-menu li:first-child:before{
content:'';
position:absolute;
left:-8px;
top:-8px;
width:0;
height:0;
border-left:5px solid transparent;
border-right:5px solid transparent;
border-bottom:8px solid #eee
}
.widget_easy_sidebar_menu_widget li.menu-item .sub-menu li:last-child{
border-bottom:0px
}
并检查返回的集合中是否有任何内容。这将防止在元素不存在的情况下发生异常
FindElements
要使用元素,请使用索引ReadOnlyCollection<IWebElement> elements = driver.FindElements(By.XPath("//[@id='view_container']/form/div[2]/div/div/div/ul/li[1]/div/div[2]"));
if (elements.Count > 0)
{
// found
}
。
答案 1 :(得分:0)
您可以执行以下操作 -
try
{
if(driver.FindElement(By.XPath("//[@id='view_container']/form/div[2]/div/div/div/ul/li[1]/div/div[2]")).Displayed)
{
// if we found xpath
}
}
catch (Exception ex)
{
// if don't find xpath
}