您好,我尝试使用xpath将值填充到文本框(检查下面的图像)。
Actions actions = new Actions(driver);
actions.moveToElement(driver.findElement(By.xpath("//*[@class='CzI8E']")));
actions.click();
Thread.sleep(3000);
actions.moveToElement(driver.findElement(By.xpath("//*[@class='_2S1VP copyable-text selectable-text']")));//_2S1VP copyable-text selectable-text
actions.sendKeys(WhatsappConstants.TEXT_MESSAGE);
actions.build().perform();
但是我遇到了这个异常
org.openqa.selenium.WebDriverException: unknown error: ChromeDriver only supports characters in the BMP
其他stackoverflow回答说要使用firefox驱动程序,但就我而言,我只需要使用chrome。
答案 0 :(得分:1)
这是Chromedriver的已知限制,请参见http://crbug.com/chromedriver/2269,以获取官方错误跟踪器中的错误报告。
您可以做的只是将自己限制为受支持的字符,基本上是来自http://www.columbia.edu/kermit/ucs2.html
的字符或者,您可以模拟输入,而不用像下面这样的前端JS代码段真正发送键:
(function (element, text) {
Array.prototype.forEach.call(text, function (char) {
element.value += char;
element.dispatchEvent(new KeyboardEvent("keydown"));
element.dispatchEvent(new KeyboardEvent("keypress"));
element.dispatchEvent(new KeyboardEvent("input"));
element.dispatchEvent(new KeyboardEvent("keyup"));
});
}).apply(null, arguments);
然后您使用JavascriptExecutor拨打电话:
((JavascriptExecutor) driver).executeScript(JS_CODE, element, text);
此代码段适用于具有可写.value
属性的元素,可以扩展为支持contenteditable
元素。
请注意,事件的字段设置为默认值,包括键码等,请参见https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent。此外,您可能还必须添加要触发的其他事件,以更好地模拟用户输入。
答案 1 :(得分:0)
我有一个类似的问题,我以这种方式解决了。
首先,您应该使用/*
* A Seed Fill Algorithm
* by Paul Heckbert
* from "Graphics Gems", Academic Press, 1990
*
* user provides pixelread() and pixelwrite() routines
*/
/*
* fill.c : simple seed fill program
* Calls pixelread() to read pixels, pixelwrite() to write pixels.
*
* Paul Heckbert 13 Sept 1982, 28 Jan 1987
*/
typedef struct { /* window: a discrete 2-D rectangle */
int x0, y0; /* xmin and ymin */
int x1, y1; /* xmax and ymax (inclusive) */
} Window;
typedef int Pixel; /* 1-channel frame buffer assumed */
Pixel pixelread(int x, int y);
void pixelwrite(int x, int y, Pixel p);
typedef struct {short y, xl, xr, dy;} Segment;
/*
* Filled horizontal segment of scanline y for xl<=x<=xr.
* Parent segment was on line y-dy. dy=1 or -1
*/
#define MAX 10000 /* max depth of stack */
#define PUSH(Y, XL, XR, DY) /* push new segment on stack */ \
if (sp<stack+MAX && Y+(DY)>=win->y0 && Y+(DY)<=win->y1) \
{sp->y = Y; sp->xl = XL; sp->xr = XR; sp->dy = DY; sp++;}
#define POP(Y, XL, XR, DY) /* pop segment off stack */ \
{sp--; Y = sp->y+(DY = sp->dy); XL = sp->xl; XR = sp->xr;}
/*
* fill: set the pixel at (x,y) and all of its 4-connected neighbors
* with the same pixel value to the new pixel value nv.
* A 4-connected neighbor is a pixel above, below, left, or right of a pixel.
*/
void fill(x, y, win, nv)
int x, y; /* seed point */
Window *win; /* screen window */
Pixel nv; /* new pixel value */
{
int l, x1, x2, dy;
Pixel ov; /* old pixel value */
Segment stack[MAX], *sp = stack; /* stack of filled segments */
ov = pixelread(x, y); /* read pv at seed point */
if (ov==nv || x<win->x0 || x>win->x1 || y<win->y0 || y>win->y1) return;
PUSH(y, x, x, 1); /* needed in some cases */
PUSH(y+1, x, x, -1); /* seed segment (popped 1st) */
while (sp>stack) {
/* pop segment off stack and fill a neighboring scan line */
POP(y, x1, x2, dy);
/*
* segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x=x1; x>=win->x0 && pixelread(x, y)==ov; x--)
pixelwrite(x, y, nv);
if (x>=x1) goto skip;
l = x+1;
if (l<x1) PUSH(y, l, x1-1, -dy); /* leak on left? */
x = x1+1;
do {
for (; x<=win->x1 && pixelread(x, y)==ov; x++)
pixelwrite(x, y, nv);
PUSH(y, l, x-1, dy);
if (x>x2+1) PUSH(y, x2+1, x-1, -dy); /* leak on right? */
skip: for (x++; x<=x2 && pixelread(x, y)!=ov; x++);
l = x;
} while (x<=x2);
}
}
向此div添加任何文本,然后清除此文本并使用硒的JavascriptExecutor
方法
sendKeys