How to sendKeys wherever the cursor is in selenium nodejs - node.js

I have created a script to comment in facebook posts, I did clicked on comment box, and cursor do appears but unable to perform .sendKeys()
here's my code:
var commentBox = driver.findElement(By.xpath('//*[#id="addComment_'+this.postIdSlice+'"]/div/div[2]/div/div/div/div[1]/div'));
actions.mouseMove(commentBox).click().perform();
driver.sleep(2000);
actions.sendKeys("Hello").perform();
but when I execute it doesnt sends 'Hello' to comment box.
PS: that postIdSlice is id of the post, no issues there just want to push/send string in that commentbox.

Use the Xpath to click on the comment box, then you can use the ActionSequence to sequnce input and ENTER key.
let action = new webdriver.ActionSequence(driver);
action.sendKeys("comment");
action.sendKeys(Keys.ENTER);
action.perform()

Related

Clickable link in the TreeDataProvider

I have created a plugin whose response are displayed as rows under TreeDataProvider. In that, among other details, i have one child as "Name" and another "url". Is it possible to have "Name" as a link on which when I click, in background it opens the url in a browser?
Updated the image after the fix. Now it's opening the link outside in a browser. Can i make only label clickable? Also, can I underline the label to show that its a link?
Yes.
You can make a tree item clickable by providing it with vscode.open command:
let treeItem: vscode.TreeItem = new vscode.TreeItem(name);
treeItem.command = {
command: 'vscode.open',
arguments: [vscode.Uri.parse(url)]
} as vscode.Command;
Finally figured it out:
element.children[0].command = {
command: 'vscode.open',
arguments: [vscode.Uri.parse('https://www.google.com')]
} as vscode.Command;

Clicking a button using Python Selenium

I am trying to automate something on Facebook using Python selenium. I want to click the post button shown in the second image. When I click the inspect in chrome on this post button, it takes me to the highlighted 'class' element shown in the third image. To click this button, I have tried the following:
#browser.find_element_by_class_name("oajrlxb2 s1i5eluu qu0x051f esr5mh6w e9989ue4 r7d6kgcz rq0escxv nhd2j8a9 pq6dq46d p7hjln8o kvgmc6g5 cxmmr5t8 oygrvhab hcukyx3x cxgpxx05 d1544ag0 sj5x9vvc tw6a2znq oqcyycmt esuyzwwr f1sip0of lzcic4wl l9j0dhe7 abiwlrkh p8dawk7l ehryuci6 bp9cbjyn beltcj47 p86d2i9g aot14ch1 kzx2olss rt8b4zig n8ej3o3l agehan2d sk4xxmp2 lrazzd5p gigivrx4 sf5mxxl7 g0qnabr5 lrwzeq9o iqfcb0g7 lsqurvkf id6903cd jq4qci2q m5l1wtfr taijpn5t sn7ne77z oqhjfihn bwm1u5wc").click()
Following is the error I am getting:
One thing that I noticed is that the div tag contains the text Post, so instead of complicating things with the class name, you can simply click on the element by identifying the text it contains. You can do it like this:
browser.find_element_by_xpath("//div[contains(text(), 'Post')]")

Python + Mechanize - Emulate Javascript button click using POST?

I'm trying to automate filling a car insurance quote form on a site:
(following the same format as the site URL lets call it: "https://secure.examplesite.com/css/car/step1#noBack")
I'm stuck on the rego section as once the rego has been added, a button needs to be clicked to perform the search and it seems this is heavy Javascript and I know mechanizer can't handle this. I'm not versed in JavaScript, but I can see that when clicking the button a POST request is made to this URL: ("https://secure.examplesite.com/css/car/step1/searchVehicleByRegNo") Please see image also.
How can I emulate this POST request in Mechanize to run the javascript? So I can see the response / interact with the response? Or is this not possible? Can I consider bs4/requests/robobrowser instead. I'm only ~4 months into learning! Thanks
# Mechanize test
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.set_handle_refresh(False) # can sometimes hang without this
res = br.open("https://secure.examplesite.com/css/car/step1#noBack")
br.select_form(id = "quoteCollectForm")
br.set_all_readonly(False) # allow everything to be written to
controlDict = {}
# List all form controls
for control in br.form.controls:
controlDict[control.name] = control.value
print("type = %s, name = %s, value = %s" %(control.type, control.name, control.value))
# Enter Rego etc "example"
br.form["vehicle.searchRegNo"] = "example"
# Now for control name = vehicle.searchRegNo, value = example
# BUT Now how do I click the button?? Simulate POST? The post url is formatted like:
# https://secure.examplesite.com/css/car/step1/searchVehicleByRegNo
Javascript POST
Solved my own problem-
Steps:
open dev tools in browser
Go to network tab and clear
interact with form element (in my case car rego finder)
click on the event that occurs from interaction
copy the exact URL, Request header data, and payload
I used Postman to quickly test the request and responses were correct / the same as the Webform and found the relevant headers
in postman convert to python requests code
Now I can interact completely with the form

How do I simulate 'Enter' Key button with Node .Js /Selenium?

This is the code snippet I am using but it doesn't click
function ClickEnter(){
driver.findElement(webdriver.By.id('select2-result-label-3')).click();
}
You can send the 'Enter' key to an element like so:
driver.findElement(By.id('select2-result-label-3')).sendKeys(driver.Key.ENTER);
Taken from answer: Node.JS selenium send key enter

Log in page does not work properly

I create a login page with user name and password. In password field I am changed to character to "*" while click the button for login I can't login
--- the following code is for password field
global var
put "shalu#123" into var
on keydown var
set the hiddentext of me to the hiddentext of me & var
put "*" after me
end keydown
---main coding
global Username, var
on openCard
put "shalu" into sUsername
put "shalu#123" into var
end openCard
on loginCheck
if field "FF1" is sUsername and field "FF2" is var then
answer "Login Successful"
--go to card "accessed"
else
answer "Details Incorrect. Please try again!"
end if
end loginCheck
is it any alternative method
You haven't mentioned what the exact problem is -- are you getting an error somewhere? One issue might be mismatched variables: you have global Username and script variable sUserName.
Also, is this all your code? If yes, you're not calling the loginCheck handler anywhere, so no comparison is taking place. Your button should be calling loginCheck.
If this is for desktop, you can also use a combination of enterInField and returnInField in the card script to trigger loginCheck when pressing those keys in either field (Note: doing this will catch enter/return messages in ALL fields, so you need to code appropriately if you have additional fields on the card).
But the button should be the main control to initiate loginCheck.
Please check if your global Username, var is correct. Shouldn't that maybe be global sUsername, var?
Also, I usually add some put sUsername or answer sUsername statements into my code so I can in the message box see what value a certain variable has at some point. Later I comment them out so they are not exectued in a delivery version

Resources