为什么我不能在此代码中单击此元素?

时间:2017-06-06 20:15:39

标签: c# selenium

Actions action = new Actions(driver); IWebElement we = driver.FindElement(By.XPath(".//*[@class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"));
action.MoveToElement(driver.FindElement(By.XPath(".//*[@class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();

期望元素如下:

< span tabindex = "-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width: 200px;"> <a tabindex = "0" class="ms-crm-Menu-Label" onclick="return false"> <img tabindex = "-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" style="vertical-align: top;" src="/_imgs/imagestrips/transparent_spacer.gif"> <span tabindex = "-1" class="ms-crm-CommandBar-Menu" [enter image description here][1]style="max-width: 150px;" command="lead|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid"> New </span> <div class="ms-crm-div-NotVisible"> Create a new Lead record. </div>
</a> </span>

请注意,当鼠标悬停时,此课程"ms-crm-CommandBar-Button ms-crm-Menu-Label"会变为"ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered"

非常感谢。

2 个答案:

答案 0 :(得分:0)

当您搜索悬停元素时,它不存在。 因此,您需要首先悬停元素,然后您才能找到并单击悬停元素。 action.MoveToElement(driver.FindElement(By.XPath("//*[@class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"))).Build().Perform().MoveToElement(driver.FindElement(By.XPath("//*[@class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();

答案 1 :(得分:0)

首先在悬停的情况下你需要移动到那个元素,之后你可以点击它。

Actions actions = new Actions(driver);
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("ur element"))).click().build().perform();