When developing a crawler project using Selenium + Chrome, it was possible to use click events to complete all operations. However, when deploying the server, the click operation kept running out of time during the click event on an image. Let me use by. id or by. name, but that element has no ID or name attributes.

And then I chose to use cssSelector to locate it, and there’s a trick here that I recommend to be lazy, but if you can’t write cssSelector and xpath syntax, you can use chrome’s own widget to get it

But if I’m using CSSSelector, it still times out, click times out, and I really want to make fun of that, but local runs are fine, they’re fast.

Afterwards big guy say, direct run inside js bar.

Then comes today’s topic, using Chrome + Selenium to run JS.

As you can see, the click time is to execute a JS method. In Selenium, there are methods to execute JS scripts.

((JavascriptExecutor) this.driver).executeScript(js);

Just pass in the JS script.

So what I did initially was I just called his method.

It will be found that such an error is reported, clientY undefined.

I looked at the error report and it looked like this.

Asked the front-end boss, only to find that the original design is to trigger the click event, and then obtain his mouse hover state, and then to set the new window position. I did not click on the event, that means there is no event, then clientY must not get.

After knowing the reason, I first thought that the solution was to let him get the mouse click event, so that he can have the event, but I thought that no, if he could click all, then there would be no timeout before. Then he rewrites his JS method so that his top property is not retrieved through clientY, but is given a defined value and calls the method again.

Try console in Chrome first:

This is the first time I’ve found that you can do this in Chrome. Chrome is a real gem, and there are a lot of tools that are really good.

Then switch to the background operation:

Run unitTest, the result is correct.

Dog lost, I left a person, a person to refueling ah.