我正在尝试让JS加载一个网站,然后点击两个按钮。第一个按钮点击并通过,但第二个按钮发出此错误
const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'commit' }),
20000
);
const GTC_Button = driver.wait(
webdriver.until.elementLocated({ xpath: '//*[@id="cart"]/a[2]' }),
20000
);
ATC_Button.click();
GTC_Button.click();
错误:
(node:21408) UnhandledPromiseRejectionWarning: WebDriverError: element not visible
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.16299 x86_64)
at Object.checkLegacyResponse (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\error.js:585:15)
at parseHttpResponse (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\http.js:533:13)
at Executor.execute (C:\New folder\JS\Bot V1\MYBot\node_modules\selenium-webdriver\lib\http.js:468:26)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
(node:21408) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:21408) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我不确定如何处理JS中的错误,我还在学习。有人可以解释一下吗?
答案 0 :(得分:2)
在selenium中#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int main (void)
{
int fd, sz;
int size; //the nuber of bytes of the file
int index = 0;
int j, m, k, i = 0;
int count, tot = 0;
int plain_counter = 0;
int encoded_counter = 0;
struct stat st;
stat("file_input.txt", &st);
size = st.st_size;
char c[size];
fd = open("file_input.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }
sz = read(fd, c, size);
printf("called read(%d, c, %i). returned that" " %d bytes were read.\n", fd, size, sz);
c[sz] = '\0';
printf("Those bytes are as follows: \n%s\n", c);
for (k = 0; k < size; k++)
{
if (c[k] == '\n')
{
index++; //countin how many times the \n appear in the file
}
}
char temp[index][1024]; //array to save modifived lines
char plain[index][512]; //array to save plain lines
char encoded[index][512]; //array to save encoded lines
//Bring the string from C[] to temp[][] omitting < >
for(j = 0 ; j < index; j++)
{
//printf("%i\n", j);
while(c[i] != '\n')
{
//printf("j = %i , k = %i ", j, k);
if(c[i] != '<' && c[i] != '>')
{
temp[j][k] = c[i];
//printf("%c", temp[j][k]); //if i print temp here it's perfect
k++;
}
i++;
}
printf("temp[%i][%i] = %c\n", j, k, temp[j][k]);
temp[j][k] = '\n';
tot += k;
k = 0;
i++;
}
temp[j][tot + 1] = '\0';
//printf("0 0 = %c\n", temp[0][0]);
i = 0;
j = 0;
k = 0;
//i print the temp array to check if it is well made
while(j < index)
{
printf("j = %i, k = %i\n", j, k);
while(temp[j][k] != '\n')
{
printf("%c", temp[j][k]); //the problem occurs here, in temp[0][k]!!
k++;
}
printf("\n");
k = 0;
j++;
}
printf("\n");
//saving the plain text into te plain array and the encoded text into te encoded array
//it works but the first line is a mess (ONLY ON MACOS)
for ( j = 0 ; j < index; j++)
{
count = 0;
i = 0;
k = 0;
m = 0;
while(temp[j][i] != '\n'){
if(temp[j][i] != ';')
{
if (count == 0)
{
plain[j][k] = temp[j][i];
printf("%c", plain[j][k]);
k++;
plain_counter++;
}else{
encoded[j][m] = temp[j][i];
printf("%c", encoded[j][m]);
m++;
encoded_counter++;
}
}
else if(temp[j][i] == ';')
{
printf("\n");
count = 1;
}
i++;
}
printf("\n");
}
plain[j][plain_counter + 1] = '\0';
encoded[j][encoded_counter + 1] = '\0';
return 0;
}
会返回driver.wait
或IThenable
。 Promise只是一种在javascript中进行异步编程的方法,它们有两个函数,Promise
和`catch,最新的是如何处理promise中的拒绝(错误)。所以,你的代码必须是:
then
为了进一步参考,我发现this文章是对promises的一个很好的介绍。
<强>更新强>
您的问题很可能是因为您之前尝试const ATC_Button = driver.wait(
webdriver.until.elementLocated({ name: 'commit' }),
20000
).catch(function(err){
// Do something with you error
});
const GTC_Button = driver.wait(
webdriver.until.elementLocated({ xpath: '//*[@id="cart"]/a[2]' }),
20000
).catch(function(err){
// Do something with you error
});
按钮,因此,由于您的click
返回driver.wait
(WebElement的承诺),因此有两个选项:
WebElementPromise
注意:这仅适用于ES6
driver
.wait(webdriver.until.elementLocated({ name: "commit" }), 20000)
.then(button => button.click()) // Here the WebElement has been returned
.catch(function(err) {
// Do something with you error
});
ps:因为你说你还在学习我认为这里的术语可能不知道是否属实,我建议你做研究。