Jump to content

autoit with webdriver on Chrome but can't get element in new tab


cPtz
 Share

Recommended Posts

Greeting everyone!

I'm new to the forum and autoit,and I'm using it to do a login which need to open new tab in a Chrome window(it's fine and I think I finish it),but when I open the new tab,the webdriver  function cannot get the same position's element,but in the first tab it get it nice and well

Did I miss anything or I use anything wrong?Thanks in advance!

Below is my code.

#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"
Local $sDesiredCapabilities
Local $account
Local $pwd
Local $value_for_account
Local $value_for_pwd
Local $login
Local $click_for_login

SetupChrome()
_WD_Startup()
_WD_ConsoleVisible(False)
$sSession1 = _WD_CreateSession($sDesiredCapabilities)
_WD_Window($sSession1,'MAXIMIZE')
_WD_Navigate($sSession1, "http://whatever.com")
if _WD_LoadWait($sSession1) == 1 Then
  $account = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div[1]/div/div/div/div/div/div[2]/form/div[1]/input")
  $value_for_account = _WD_ElementAction($sSession1, $account, "CLICK")
  Send("admin1")
  $pwd = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div[1]/div/div/div/div/div/div[2]/form/div[2]/div[1]/input")
  $value_for_pwd = _WD_ElementAction($sSession1, $pwd, "CLICK")
  Send("1234")
  $login = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div/div/div/div/div/div/div[2]/form/button")
  $click_for_login = _WD_ElementAction($sSession1, $login, "click" )
  _WD_NewTab($sSession1)
  _WD_Navigate($sSession1, "http://whatever.com")
  if _WD_LoadWait($sSession1) == 1 Then
    $account = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div[1]/div/div/div/div/div/div[2]/form/div[1]/input")
    $value_for_account = _WD_ElementAction($sSession1, $account, "CLICK")
    Send("admin1")
    $pwd = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div[1]/div/div/div/div/div/div[2]/form/div[2]/div[1]/input")
    $value_for_pwd = _WD_ElementAction($sSession1, $pwd, "CLICK")
    Send("1234")
    $login = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/div/div/div/div/div/div/div[2]/form/button")
    $click_for_login = _WD_ElementAction($sSession1, $login, "click" )
  EndIf
EndIf
_WD_ShutDown()
Func SetupChrome()
_WD_Option('Driver',"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\chromedriver.exe")
_WD_Option('Port', 9515)
;_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation", "enable-logging"]}}}}'
EndFunc

 

Link to comment
Share on other sites

It's difficult to know for sure since we can't run your code and we can't examine the website. Have you tried using relative xpath instead of absolute xpath?

FWIW, this is the difference in xpath styles using the Submit Reply button from the website --

Absolute

/html[1]/body[1]/main[1]/div[1]/div[1]/div[1]/div[4]/div[3]/form[1]/div[1]/div[2]/ul[1]/li[2]/button[1]

Relative

//button[@role='button']

P.S. There are several tools listed on the wiki page that can help you obtain these values.

Link to comment
Share on other sites

Hi danp2,thanks for reply

my test website URL is change since is private and I will try another that can run for all you

and for absolute xpath issue,yes I have try using normal Xpath before,but my code will stop working so I change it into absolute xpath(I don't know if it's double quotation mark causing the problem or some other issue)

Link to comment
Share on other sites

I did some modification on the code,don't know if it's helpful.

I also found that my question is a little not clear,so I will put more info hrer:

the first tab that I open can be control(I can enter text to the input box,drop down list,etc...),but when it comes to the second tab I open under the same window(and the URL is as same as the first tab),it can't find any element in the second tab (and will enter everything in the URL area in my private website)

Below is the code I modify,it should run into the problem as I said.

#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"
Local $sDesiredCapabilities
Local $account
Local $pwd
Local $value_for_account
Local $value_for_pwd
Local $login
Local $click_for_login

SetupChrome()
_WD_Startup()
_WD_ConsoleVisible(False)
$sSession1 = _WD_CreateSession($sDesiredCapabilities)
_WD_Window($sSession1,'MAXIMIZE')
_WD_Navigate($sSession1, "http://vbsca.ca/login/login.asp")
if _WD_LoadWait($sSession1) == 1 Then
  $account = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[1]/td[2]/input")
  $value_for_account = _WD_ElementAction($sSession1, $account, "CLICK")
  Send("admin1")
  $pwd = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[2]/td[2]/input")
  $value_for_pwd = _WD_ElementAction($sSession1, $pwd, "CLICK")
  Send("1234")
  $login = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[3]/td[2]/input")
  $click_for_login = _WD_ElementAction($sSession1, $login, "CLICK" )
  _WD_NewTab($sSession1)
  _WD_Navigate($sSession1, "http://vbsca.ca/login/login.asp")
  if _WD_LoadWait($sSession1) == 1 Then
    $account = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[1]/td[2]/input")
    $value_for_account = _WD_ElementAction($sSession1, $account, "CLICK")
    Send("admin1")  
    $pwd = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[2]/td[2]/input")
    $value_for_pwd = _WD_ElementAction($sSession1, $pwd, "CLICK")
    Send("1234")
    $login = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[3]/td[2]/input")
    $click_for_login = _WD_ElementAction($sSession1, $login, "CLICK" )
  EndIf
EndIf
_WD_ShutDown()
Func SetupChrome()
_WD_Option('Driver',"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\chromedriver.exe")
_WD_Option('Port', 9515)
;_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation", "enable-logging"]}}}}'
EndFunc

 

Link to comment
Share on other sites

Just noticed that you are trying to use Send(). This is your issue. Try using _WD_SetElementValue instead.

#include "wd_core.au3"
#include "wd_helper.au3"
#include "wd_capabilities.au3"
Local $sDesiredCapabilities
Local $account
Local $pwd
Local $value_for_account
Local $value_for_pwd
Local $login
Local $click_for_login

SetupChrome()
_WD_Startup()
_WD_ConsoleVisible(False)
$sSession1 = _WD_CreateSession($sDesiredCapabilities)
_WD_Window($sSession1,'MAXIMIZE')
_WD_Navigate($sSession1, "http://vbsca.ca/login/login.asp")
if _WD_LoadWait($sSession1) == 1 Then
  Login()
  
  _WD_NewTab($sSession1)
  _WD_Navigate($sSession1, "http://vbsca.ca/login/login.asp")
  if _WD_LoadWait($sSession1) == 1 Then
    Login()
  EndIf
EndIf
_WD_ShutDown()

Func Login()
  $account = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[1]/td[2]/input")
  _WD_SetElementValue($sSession1, $account, "admin1")
  
  $pwd = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[2]/td[2]/input")
  _WD_SetElementValue($sSession1, $pwd, "1234")
  
  $login = _WD_FindElement($sSession1,$_WD_LOCATOR_ByXPath,"/html/body/form/table/tbody/tr[3]/td[2]/input")
  $click_for_login = _WD_ElementAction($sSession1, $login, "CLICK" )
EndFunc

Func SetupChrome()
_WD_Option('Driver',"chromedriver.exe")
_WD_Option('Port', 9515)
;_WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')
$sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"w3c": true, "excludeSwitches": [ "enable-automation", "enable-logging"]}}}}'
EndFunc

 

Edited by Danp2
Link to comment
Share on other sites

thanks Danp2,it works perfectly,my issue solved!

But I still confuse on a little problem.If the element I want to text in has a initial data in,I use for example,if the account element already got text '123'in it and I use(_WD_SetElementValue($sSession,account,''),then I will got account:123 but not account: 

Am I on the right track using this function?because I see the using _WD_SetElementValue wont delete the initial value in the input box(I've try,didn't work)

Link to comment
Share on other sites

wait,maybe I found a solution,I post it here to let people check if its good or not.

use _WD_ElementActionEx($sSession,$sElement,'Hide') and the initial text on the elemnt will disappear and I can text in whatever I want.That is my solution for now

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...