[英]Not able to click on an option of a table
I am trying to automate a task that uses a third party website. 我正在尝试自动化使用第三方网站的任务。 In this website, we have to select an option from a list of items.
在此网站中,我们必须从项目列表中选择一个选项。
It is a text area, that when it is clicked, JS creates a table with options to take. 这是一个文本区域,当单击该区域时,JS会创建一个带有选项的表格。 I have been able to click on the text area to show this list and can correctly identify the items in the list.
我已经能够单击文本区域以显示此列表,并且可以正确识别列表中的项目。 The problem is that when I click on the items, nothing is happening.
问题是,当我单击这些项目时,没有任何反应。
The table looks like this: 该表如下所示:
<div class="MenuTableContainer">
<table class="MenuTable" style="width: 175px;" cellSpacing="0" cellPadding="0">
<tbody class="MenuTableBody">
<tr class="MenuTableRow">
<td class="MenuEntryName" noWrap="">1-Extensive/Widespread</td>
<td class="MenuEntryNoSub" ARValue="1-Extensive/Widespread"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" noWrap="">2-Significant/Large</td>
<td class="MenuEntryNoSub" ARValue="2-Significant/Large"></td>
</tr>
<tr class="MenuTableRow">
<td class="MenuEntryName" noWrap="">(clear)</td>
<td class="MenuEntryNoSub" ARValue=""></td></tr>
</tbody>
</table>
</div>
The input field that I am trying to fill out looks like this without any values: 我尝试填写的输入字段看起来像这样,没有任何值:
<div class="df arfid1000000163 ardbnImpact EnumSel" id="WIN_2_1000000163" style="left: 10px; top: 82px; width: 292px; height: 21px; z-index: 990;" ardbn="Impact" artype="EnumSel" arid="1000000163" arlbox="0,4,112,17" arwindowid="2">
<label class="label f9" id="label1000000163" style="left: 0px; top: 4px; width: 112px; height: 17px;" for="x-arid_WIN_2_1000000163">
Impact*
</label>
<div class="selection" style="left: 117px; top: 0px; width: 175px; height: 21px;" arselmenu='[{ci:1000,v:"1-Extensive/Widespread"},{ci:2000,v:"2-Significant/Large"},{ci:3000,v:"3-Moderate/Limited"},{ci:4000,v:"4-Minor/Localized"}]'>
<input title="" class="text " id="arid_WIN_2_1000000163" style="left: 0px; top: 0px; width: 154px; height: 21px;" type="text" readOnly="">
<a class="btn btn3d selectionbtn" style="left: 154px; top: 0px; width: 21px; height: 21px;" href="javascript:">
<img class="btnimg" alt="" src="../../../../resources/images/mt_sprites.gif">
</a>
</div>
</div>
And like this once a value has been selected: 并选择了以下值:
<div class="df arfid1000000163 ardbnImpact EnumSel" id="WIN_2_1000000163" style="left: 10px; top: 82px; width: 292px; height: 21px; z-index: 990;" ardbn="Impact" artype="EnumSel" arid="1000000163" arlbox="0,4,112,17" arwindowid="2">
<label class="label f9" id="label1000000163" style="left: 0px; top: 4px; width: 112px; height: 17px;" for="x-arid_WIN_2_1000000163">
Impact*
</label>
<div class="selection" style="left: 117px; top: 0px; width: 175px; height: 21px;" arselmenu='[{ci:1000,v:"1-Extensive/Widespread"},{ci:2000,v:"2-Significant/Large"},{ci:3000,v:"3-Moderate/Limited"},{ci:4000,v:"4-Minor/Localized"}]'>
<input title="2-Significant/Large" class="text " id="arid_WIN_2_1000000163" style="left: 0px; top: 0px; width: 154px; height: 21px;" type="text" readOnly="">
<a class="btn btn3d selectionbtn" style="left: 154px; top: 0px; width: 21px; height: 21px;" href="javascript:">
<img class="btnimg" alt="" src="../../../../resources/images/mt_sprites.gif">
</a>
</div>
</div>
The only difference is the value of the title attribute in the input tag. 唯一的区别是输入标签中title属性的值。
I tried setting the attribute (and reading it afterwards to confirm), but once the save button was clicked, it failed because the field was empty: 我尝试设置属性(然后读取该属性以进行确认),但是一旦单击保存按钮,该操作将失败,因为该字段为空:
sImpactString="2-Significant/Large";
oImpactField.setAttribute("title", sImpactString);
string attr = oImpactField.getAttribute("title");
I also tried clicking on the TR and TD tags without success. 我也尝试单击TR和TD标签,但未成功。 I do not see any reference to a JS function in the HTML, so I am a little lost in terms of what I am missing here.
我没有在HTML中看到对JS函数的任何引用,因此我在这里所缺少的内容上有些失落。
I am using SHDocVw.InternetExplorer and IHtmlElement/IHtmlElementCollection. 我正在使用SHDocVw.InternetExplorer和IHtmlElement / IHtmlElementCollection。
This is how I am getting to the TD and TR tags: 这就是我到达TD和TR标签的方式:
IHTMLDocument3 document = driver.Document as IHTMLDocument3;
IHTMLElement oTableBody = null;
bool settingsShown = false;
do
{
IHTMLElementCollection tBodys = document.getElementsByTagName("tbody");
foreach (IHTMLElement elem in tBodys)
{
if (string.Compare(elem.className, "MenuTableBody") == 0)
{
settingsShown = true;
oTableBody = elem;
break;
}
}
} while (!settingsShown);
int childrenPos = (int)impactRating;
// Get the TR
IHTMLElement oCorrectValue = oTableBody.children[0] as IHTMLElement;
// Get the first TD
IHTMLElement oColumn = oCorrectValue.children[0] as IHTMLElement
//Clicking does nothing on the TR
oCorrectValue.click()
//Nor in the TD
oColumn.click()
After looking into the JS I have encountered these two functions. 在研究了JS之后,我遇到了这两个函数。 The first one handles the mouse events, and the second ones tries to click on an item
第一个处理鼠标事件,第二个尝试单击项目
MenuElement.prototype.HandleMouseEvent = function (d, h) {
var g = h.element;
var f = g.className;
if (f == null) {
return
}
if (d == 1) {
if (g.tagName != "TD" || f.indexOf("MenuEntry") == -1) {
return
}
this.EndScroll();
g = g.parentNode.firstChild;
this.HighlightElement(g);
this.CheckOpenSubMenu()
}
else {
if (d == 0) {
if (f.indexOf("MenuEntry") != -1) {
OpenedMenu.CheckClick()
}
}
else {
if (d == 2) {
if (g.tagName != "DIV" && (f != "MenuScrollDown" || f != "MenuScrollUp")) {
return
}
var b = (f == "MenuScrollUp") ? 1 : -1;
OpenedMenu.EnsureTopElement(this.mSerial);
this.Scroll(b)
}
else {
if (d == 3) {
if (g.tagName != "DIV" && (f != "MenuScrollDown" || f != "MenuScrollUp")) {
return
}
OpenedMenu.EnsureTopElement(this.mSerial);
this.EndScroll()
}
else {
}
}
}
}
return
}
TableContextMenu.prototype.CheckClick = function () {
var d = this.mElementStack;
this.mCtrlFldId = null;
var b = d[d.length - 1].mHighlightedElement;
if (b != null && b.nextSibling != null) {
this.mCtrlFldId = b.nextSibling.getAttribute("arbtnid")
}
TableContextMenu.superclass.CheckClick.call(this)
}
It is now clear to me that I need to interact with the TDs in the table. 现在我很清楚,我需要与表中的TD进行交互。 However, I am not sure if I need to highlight an element first.
但是,我不确定是否需要首先突出显示一个元素。 To highlight them, I just move my mouse over it.
要突出显示它们,我只需将鼠标移到它上面。 Is there a way to recreate this?
有没有办法重新创建它? And then I would just need to click on the TD?
然后,我只需要单击TD?
I have tried the following code on the MenuEntryName (since that is the one that is highlighted anyway), and it is not highlighting it. 我已经在MenuEntryName上尝试了以下代码(因为无论如何都将其突出显示),但它并未突出显示。
IHTMLElement entryName = desiredRow.children[0] as IHTMLElement;
entryName.onclick = entryName.onmouseover;
entryName.click();
Once it is highlighted, the TD's class change to "MenuEntryNameHover" and "MenuEntryNoSubHover" respectively. 突出显示后,TD的类将分别更改为“ MenuEntryNameHover”和“ MenuEntryNoSubHover”。 Which is not happening.
这没有发生。
I ended up not worrying about the JS and just sending keys to the window like this: 我最终不必担心JS,而只是将密钥发送到窗口,如下所示:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
public void MyMethod(InternetExplorer driver)
{
//.....
while (currentPos++ <= nPosition)
{
SetForegroundWindow((IntPtr)driver.HWND);
System.Windows.Forms.SendKeys.SendWait(Down_Key);
Task.Delay(100).Wait();
}
SetForegroundWindow((IntPtr)driver.HWND);
System.Windows.Forms.SendKeys.SendWait(Enter_Key);
SetForegroundWindow((IntPtr)driver.HWND);
System.Windows.Forms.SendKeys.Flush();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.