简体   繁体   English

如何在HTML表格日历中单击内部元素

[英]How to click on internal element in an HTML table calendar

I'm using selenium web-driver 3.6.0 version + C#. 我正在使用Selenium Web驱动程序3.6.0版本+ C#。 I try to click on the calendar to select different dates. 我尝试单击日历以选择其他日期。 The calendar has a drop-down lists to select year and month. 日历具有用于选择年份和月份的下拉列表。 When I need to select year, I should click on arrow near the text of current year, and select from list other year. 当我需要选择年份时,我应该单击当前年份文本附近的箭头,然后从其他年份列表中进行选择。 If my wanted year doesn't present in the list, I should be click on "-"(minus) or on "+"(plus) and the list rolls up/down and displays other years. 如果我想要的年份没有出现在列表中,则应单击“-”(减号)或“ +”(加号),列表将向上/向下滚动并显示其他年份。

When I try to click on plus or on minus from the list, I received MSG: "element not visible" 当我尝试从列表中单击加号或减号时,我收到了味精:“元素不可见”
I tried to search a lot about this problem, and even tried all the suggested ways, but I could not solve the problem. 我尝试搜索有关此问题的很多内容,甚至尝试了所有建议的方法,但无法解决问题。

I'll detail what I tried: 我将详细说明我尝试的方法:
1. basic: 1.基本:

BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();

Result: element not visible. 结果:元素不可见。

2. Click twice on element: 2.在元素上单击两次:

BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();
BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]")).Click();

Result: element not visible. 结果:元素不可见。

3. Maximize the browser window: 3.最大化浏览器窗口:

driverUser.Manage().Window.Maximize();

Result: element not visible 结果:元素不可见

4. Click via java-script: 4.通过Java脚本单击:

IWebElement minusButtonInCalendar = BrowserFactory.DriverUser.FindElement(By.XPath("//div[@id='selectYear']//tr[1]"));
((IJavaScriptExecutor)BrowserFactory.DriverUser ).ExecuteScript("arguments[0].click();" , minusButtonInCalendar);

Result: Running does not end. 结果:运行不会结束。

5. Scroll into view of the element and then click: 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();

Result: Running does not end 结果:运行不会结束

6. Move to element and then click via browser.actions(): 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();

Result: Running does not end 结果:运行不会结束

Note: when I tried recording this case in selenium IDE, it didn't record this action. 注意:当我尝试在Selenium IDE中记录这种情况时,它没有记录此操作。

The related HTML code is: 相关的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>

在此处输入图片说明

It looks like you keep trying to click the TR instead of the TD. 好像您一直尝试单击TR而不是TD。 I would try the XPath 我会尝试XPath

//div[@id='selectYear']//td[.='-']

You may need to wait for the element to be visible/clickable also. 您可能还需要等待元素也可见/可单击。 You could add something like 您可以添加类似

new WebDriverWait(Driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@id='selectYear']//td[.='-']")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM