刚刚开始没有把滚动文字停住,可以通过xpath获取到滚动文字的webelment,也可以通过getText()获取到滚动文字的文本,但是却无法点击到。最后在zhz的帮助下,找到解决办法:先用js停住滚动文字,再去做点击等操作。
实现滚动文字的js是这样的:
function scrollmarquee(){
if (_marquee_iedom){
if (parseInt(cross_marquee.style.top)>(_marquee_actualwidth*(-1)+8))cross_marquee.style.top=parseInt(cross_marquee.style.top)-_marquee_copyspeed;
else cross_marquee.style.top=marqueeheight+8;
}else if (document.layers){
if (ns_marquee.left>(_marquee_actualwidth*(-1)+8))ns_marquee.left-=_marquee_copyspeed;
else ns_marquee.left=marqueewidth+8;
}
}
_marquee_iedom是一个全局变量。所以,就在代码中使用js把这个变量置为false,先把滚动文字停住,代码如下:
String js = "_marquee_iedom=false;";
((JavascriptExecutor) driver).executeScript(js);
把滚动文字停住了,你就可以对这些元素进行操作了。
从这个问,还能学到两点,但是还有点模糊,希望大家可以指教:
1、webdriver获取页面元素的时候,是针对整个页面的html源码去获取的。
2、webdriver点击某个页面元素的时候,是先通过webelment实例去定位元素位置,然后去点击。要是元素没有显示在当前界面上,webdriver会把这个元素先拉上去到当前界面,然后在点击。
感谢为我解答的朋友:
IeDriverServer的实现原理就是找到XY的坐标,然后模式鼠标点击的动作,所以必须让网页静止。
如果在不可见区域,它会自己把控件挪到可见区域。
int Element::Click(const ELEMENT_SCROLL_BEHAVIOR scroll_behavior) {
LOG(TRACE) << "Entering Element::Click";
long x = 0, y = 0, w = 0, h = 0;
int status_code = this->GetLocationOnceScrolledIntoView(scroll_behavior, &x, &y, &w, &h);
if (status_code == SUCCESS) {
long click_x;
long click_y;
GetClickPoint(x, y, w, h, &click_x, &click_y);
// Create a mouse move, mouse down, mouse up OS event
LRESULT result = mouseMoveTo(this->containing_window_handle_,
/* duration of move in ms = */ 10,
x,
y,
click_x,
click_y);
if (result != SUCCESS) {
LOG(WARN) << "Unable to move mouse, mouseMoveTo returned non-zero value";
return static_cast<int>(result);
}
result = clickAt(this->containing_window_handle_,
click_x,
click_y,
MOUSEBUTTON_LEFT);
if (result != SUCCESS) {
LOG(WARN) << "Unable to click at by mouse, clickAt returned non-zero value";
return static_cast<int>(result);
}
//wait(50);
} else {
LOG(WARN) << "Unable to get location of clicked element";
}
return status_code;
}