[英]How to click on internal element in an HTML table calendar
我正在使用Selenium Web驱动程序3.6.0版本+ C#。 我尝试单击日历以选择其他日期。 日历具有用于选择年份和月份的下拉列表。 当我需要选择年份时,我应该单击当前年份文本附近的箭头,然后从其他年份列表中进行选择。 如果我想要的年份没有出现在列表中,则应单击“-”(减号)或“ +”(加号),列表将向上/向下滚动并显示其他年份。
当我尝试从列表中单击加号或减号时,我收到了味精:“元素不可见”
我尝试搜索有关此问题的很多内容,甚至尝试了所有建议的方法,但无法解决问题。
我将详细说明我尝试的方法:
1.基本:
BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();
结果:元素不可见。
2.在元素上单击两次:
BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();
BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();
结果:元素不可见。
3.最大化浏览器窗口:
driverUser.Manage().Window.Maximize();
结果:元素不可见
4.通过Java脚本单击:
IWebElement minusButtonInCalendar = BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]"));
((IJavaScriptExecutor)BrowserFactory.DriverUser ).ExecuteScript("arguments[0].click();" , minusButtonInCalendar);
结果:运行不会结束。
5.滚动到该元素的视图,然后单击:
IWebElement minusButtonInCalendar = BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]"));
((IJavaScriptExecutor)BrowserFactory.DriverUser ).ExecuteScript("arguments[0].scrollIntoView(true);" , minusButtonInCalendar);
BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[2]")).Click();
结果:运行不会结束
6.移至元素,然后通过browser.actions()单击:
WebDriverWait wait = new WebDriverWait(BrowserFactory.DriverUser , TimeSpan.FromSeconds(10));
var element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@id='selectYear']//tr[1]")));
new Actions(BrowserFactory.DriverUser).MoveToElement(element).Perform();
结果:运行不会结束
注意:当我尝试在Selenium IDE中记录这种情况时,它没有记录此操作。
相关的HTML代码为:
<div id="selectYear" style="position: absolute; visibility: hidden; z-index: 100001; top: 26px; left: 143px;">
<table style="font-family:arial; font-size:11px; border-width:1; border-style:solid; border-color:#a0a0a0;" onmouseover="clearTimeout(timeoutID2)" onmouseout="clearTimeout(timeoutID2);timeoutID2=setTimeout(" popDownYear() ",100)" width="44" cellspacing="0" bgcolor="#FFFFDD">
<tbody>
<tr>
<td onmouseover="this.style.backgroundColor=" #FFCC99 "" onmouseout="clearInterval(intervalID1);this.style.backgroundColor=" "" style="cursor: pointer;" onmousedown="clearInterval(intervalID1);intervalID1=setInterval(" decYear() ",30)" onmouseup="clearInterval(intervalID1)" align="center">-</td>
</tr>
</tbody>
</table>
</div>
好像您一直尝试单击TR而不是TD。 我会尝试XPath
//div[@id='selectYear']//td[.='-']
您可能还需要等待元素也可见/可单击。 您可以添加类似
new WebDriverWait(Driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@id='selectYear']//td[.='-']")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.