Please Help! Autohotkey, Chrome and Selenium Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mikea105
Posts: 8
Joined: 04 Nov 2016, 11:51

Please Help! Autohotkey, Chrome and Selenium

21 Mar 2019, 09:24

I have an AHK script that automates working with a program that runs in a web browser. I’ve gotten it working with IE. I need to get it working with Chrome, which requires Selenium. I’ve figured out most of what I need, but I’m really stuck trying to handle writing to dropdowns.

I can get the value of a dropdown with something like
Value := driver.findElementsByName(“customerAddress.state”).Attribute(“selectedIndex”)

However, nothing I try with Selenium will WRITE the index or value. Has anyone out there cracked this one? There is a library for Java that handles dropdowns, so I’ve tried various combinations like

driver.executeScript("setSelectValue( document.getElementsByName( ""customerAddress.state"")[0], '20' )") but it doesn’t work at all.

Any help would be GREATLY appreciated!
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Please Help! Autohotkey, Chrome and Selenium

21 Mar 2019, 09:28

For selecting things using javascript you want document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true
The above should select the 21st option (since javascript starts at index 0)
mikea105
Posts: 8
Joined: 04 Nov 2016, 11:51

Re: Please Help! Autohotkey, Chrome and Selenium

21 Mar 2019, 14:21

Thanks for your reply!

Based on what you said, I tried this:
driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true )")

However, it throws an exception:

Error: 0xA00A000D-
Source: Selenium
Description: UnknownError
unknown error:Runtime.evaluate threw exception: SyntaxError: Unexpected token
)
[some details about the platform and IP address)

HelpFile: (null)
HelpContext: 0
Specifically: executeScript


Any ideas where I can go from here?
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Please Help! Autohotkey, Chrome and Selenium

21 Mar 2019, 14:28

my suggestion is to try javascript in the url before putting it into the script (note that with chrome you need to add "javascript:" before the actual code for testing).
If customerAddress.State is a variable in your script and you are trying to select the option of that state you probably either need the exact name (or close to it) or the exact number in the dropdown. You can try document.getElementById('YourIDHere').options.namedItem(customerAddress.State).selected = true
If you can post the website or html as this can provide more helpful suggestions.
mikea105
Posts: 8
Joined: 04 Nov 2016, 11:51

Re: Please Help! Autohotkey, Chrome and Selenium

22 Mar 2019, 09:55

I'm sorry, I think I'm not being clear about my problem. I have no problem dereferencing the variable. For example, this works just fine and returns the index:
A_Value := driver.findElementByName("customerAddress.state").Attribute("selectedIndex")

and this returns the value:
A_Value := driver.findElementByName("customerAddress.state").Attribute("value")

So I'm referencing the correct bits and can read them okay. But how in the world can I WRITE them? Trying to set the value with either of the above just does nothing. For example, this doens't work
driver.findElementByName("customerAddress.state").Attribute("selectedIndex") := New_Index

Trying to use executeScript usually bombs out or does nothing. I'm not all that well versed on Java, and it says "syntax error" so I figure I'm doing something wrong, hopefully something small All I need to do is to be able to SET the selected index of a dropdown. I must be missing something somewhere, but I have no idea where I'm going wrong.

Thanks for your patience and help!
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Please Help! Autohotkey, Chrome and Selenium  Topic is solved

22 Mar 2019, 11:16

That first one is close. The javascript for selecting a dropdown is document.getElementById("customerAddress.state").selectedIndex = 1, this selects the dropdown index of 2 (since javascript starts at index 0) for element with id "customerAddress.state"

This isn't tested but driver.findElementByName("customerAddress.state").Attribute("selectedIndex") := 1 should select either the 2nd or first entry (depending on whether it adjust to the index 0 thing or not)

Also on the driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true )") the reason why it gives an error is because of the extra ) so it should be driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true") and it shouldn't give an error.
mikea105
Posts: 8
Joined: 04 Nov 2016, 11:51

Re: Please Help! Autohotkey, Chrome and Selenium

22 Mar 2019, 17:14

Just FYI, the first one doesn't work. It throws a "Member not found. Specifically: Attribute) error.
driver.findElementByName("customerAddress.state").Attribute("selectedIndex") := 1

HOWEVER, this one
driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true")
WORKS PERFECTLY!

THANK YOU, THANK YOU, THANK YOU VERY MUCH!!! :bravo:

I've spent days trying to get this to work, and was about to give up. Again, THANKS! :dance:
User avatar
ankitkraken
Posts: 84
Joined: 01 Jun 2019, 08:47
Location: India

Re: Please Help! Autohotkey, Chrome and Selenium

04 Jul 2019, 07:33

MannyKSoSo wrote:
22 Mar 2019, 11:16
That first one is close. The javascript for selecting a dropdown is document.getElementById("customerAddress.state").selectedIndex = 1, this selects the dropdown index of 2 (since javascript starts at index 0) for element with id "customerAddress.state"

This isn't tested but driver.findElementByName("customerAddress.state").Attribute("selectedIndex") := 1 should select either the 2nd or first entry (depending on whether it adjust to the index 0 thing or not)

Also on the driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true )") the reason why it gives an error is because of the extra ) so it should be driver.executeScript("document.getElementsByName( ""customerAddress.state"")[0].options[20].selected=true") and it shouldn't give an error.
I am facing a similar problem, I want to select a value from a drop down and your solution is not working for me.
I am able to click on the drop down using the following code.

Code: Select all

ele := driver.findElementByxpath("//*[@id='a-autoid-0-announce']")
MsgBox % ele.text
MsgBox % ele.executeScript("return this.outerHTML")
driver.ExecuteScript("arguments[0].click();", ele)
But i want to select the value "TEST" from the dropdown. How can i do that? Please help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jollyjoe and 317 guests