simonradier / node-red-contrib-simple-webdriver

SimpleWebdriver nodes for Node-RED initially based on node-red-contrib-selenium-wd2
Apache License 2.0
14 stars 8 forks source link

could you please make the "Find Elements" avaliable? #29

Closed rexchoi1983 closed 1 year ago

rexchoi1983 commented 1 year ago

hi simon,

thanks to your contrib! but in my case, i have to get elements in a website. i see the argument names multiple in function "findElement" in webdriver.js. i try to clone your repository, and add node "find elements" by modifing the copy of find-element.html and find-element.ts. but i don't know how to compile dist. i am so sorry i am not familiar with create node for node-red. my temporary way is modifing the npm module that change "multiple" default value from false to true. but that is not a good way... so could u help me out, or publish a new version for that issue? thank u for all.

image
simonradier commented 1 year ago

Hi @rexchoi1983,

Could you detail a little bit more what you need. I'm not sure you'll need to rebuild my package.

Best regards,

rexchoi1983 commented 1 year ago

hi simon,

thanks for replying me. for example, while i search something in google, it will return multiple records. just like while i use xpath like "//div[@classs='yuRUbf']", i should get an array. but the "find element" mode just return first element which xpath found, i need that array. now it works by changing parameter of "multiple" from false to true.

and i have another advice. while i use chinese character in xpath, webdriver-manager will return a bad request(400) error. for example "//a[@placeholder='请输入密码']", i will get that error. because while http request send it with utf-8 encoding, this chinese character encode to 3 bytes for on character. so i modified call function in http-client.js, encodes json string into unicode string.

thanku simon!

image
simonradier commented 1 year ago

Hi @rexchoi1983,

For the first requirement which is the ability to use "findElements" there is a small cheat which allow you to use it without the need to rebuild the packages.

Here an nodered flow exemple (to import) :

[{"id":"2137b1ab4093545e","type":"tab","label":"Flow Find Elements","disabled":false,"info":"","env":[]},{"id":"a9010e135b3f62cc","type":"inject","z":"2137b1ab4093545e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":160,"wires":[["f2774f1ccf1d82b3"]]},{"id":"f2774f1ccf1d82b3","type":"open browser","z":"2137b1ab4093545e","name":"","browserType":"chrome","webURL":"https://flows.nodered.org/","width":1280,"height":1024,"timeout":3000,"maximized":true,"headless":true,"args":["--headless"],"serverURL":"http://localhost:9515","x":330,"y":160,"wires":[["da5e1e6f6008d216"]]},{"id":"da5e1e6f6008d216","type":"function","z":"2137b1ab4093545e","name":"Find Elements","func":"try {\n    let elements = await msg.browser.findElements(\n                                                'xpath',\n                                                '//li[contains(@class, \"gistbox\")]',\n                                                1000\n                                            );\n    msg.elements = elements;\n    return [msg, null];\n} catch (e) {\n    msg.error = e\n    return [null, msg];\n}","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":540,"y":160,"wires":[["71d89d70c359dcec","2bc0c83e28a6bbee"],["71d89d70c359dcec"]]},{"id":"71d89d70c359dcec","type":"close browser","z":"2137b1ab4093545e","name":"","waitFor":500,"x":810,"y":160,"wires":[[]]},{"id":"2bc0c83e28a6bbee","type":"debug","z":"2137b1ab4093545e","name":"List of elements","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"elements","targetType":"msg","statusVal":"","statusType":"auto","x":820,"y":60,"wires":[]}]

It uses a function node to use the browser member of msg (which is required by the node to work).

I'll update the node to allow the use of findElements API in a more "standard way"

For the chinese characters, could you give me a nodered flow example which generate the error?

Best regards,

rexchoi1983 commented 1 year ago

hi simon,

thanks for your advice, that's very helpful!

i upload a sample flow, please try it. you could see error message in console of nodered.

image

i use wireshark to capture package, problem is here:

image

if i encode character into unicode string, it will work fine:

image

i am not very good at nodejs, maybe that is not a good way to fixit. it's just my little suggestion. i wish it can be helpful.

thanks again!!

sample_flow.json.txt

simonradier commented 1 year ago

Thanks! I'll try to correct this next week.

rexchoi1983 commented 1 year ago

thank u simon! looking forward to the next version!

simonradier commented 1 year ago

@rexchoi1983 : the new 0.2.2 version has corrected the chinese character error. It was quite simple, the following code was not correct : httpOptions.headers['Content-Length'] = sBody.length, it was counting the number of characters, not the number of bytes. The update httpOptions.headers['Content-Length'] = new Blob([sBody]).size corrected this.

Could you try and tell me if it is ok on your side?

Ps : Still working on an update to expose findElements.

rexchoi1983 commented 1 year ago

hi, simon:

the new version fixes chinese character well. that's great! it indeed a very simple way to fix, so cool!! and i also expect the new version for updating feature of "find elements" . thanks again, simon!

simonradier commented 1 year ago

Hi @rexchoi1983 ,

I made a new 0.3.0 version. I'll let you do some test.

Tell me if it works as expected.

Best regards,