Jump to content

Can I let Webdriver open a new tab in Chrome?


Paulliev
 Share

Recommended Posts

Thanks for your reply!

I have got the following code. The problem is that it opens a new tab in the already existing Chrome, so that's good, but it won't navigate to the website. See the code below. 

Thanks in advance!

#include "wd_core.au3"
#include "wd_helper.au3"

$_WD_DEBUG = $_WD_DEBUG_None
Local $sDesiredCapabilities, $sSession

ShellExecute("chrome.exe", "--remote-debugging-port=9222")

SetupChrome()
_WD_Startup()

; Create session and log in
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_Navigate($sSession, "https://app.teamleader.eu/tickets.php")

; Create new ticket
_ChromeClickButtonId($sSession, 'loginButton')
Sleep(5000)
_ChromeClickButtonClass($sSession, '_3F4D5 _3FpQy _1ff4h _2Yigy _1-T6- e6RGs _31W-e')
Sleep(2000)
_ChromeSetInputValueById($sSession,'id_corc_id', ClipGet())
Sleep(1000)

$sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='suggestions_corc_id']/a[@id='sg_corc_id_0']")
$title = _WD_ElementAction($sSession, $sLink, 'attribute', 'title')

If $title == "-3" Then 
    MsgBox(0, "Helaas", "Deze klant staat niet in het systeem! Ga alsjeblieft zelf verder met invoeren!", 100)
Else
    _ChromeClickDropdown($sSession, 'sg_corc_id_0')
EndIf

_WD_Shutdown()

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"debuggerAddress": "localhost:9222"} {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}'
EndFunc

 

Link to comment
Share on other sites

You would need to create a new tab :

_WD_Startup()
Local $sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_NewTab($sSession)
_WD_Navigate($sSession, "https://app.teamleader.eu/?gotologin")

 

Link to comment
Share on other sites

@Danp2 See my full code below. It should run now and you can see that the WD_Navigate won't work. 

#include "wd_core.au3"
#include "wd_helper.au3"

$_WD_DEBUG = $_WD_DEBUG_None
Local $sDesiredCapabilities, $sSession

ShellExecute("chrome.exe", "--remote-debugging-port=9222")

SetupChrome()
_WD_Startup()

; Create session and log in
$sSession = _WD_CreateSession($sDesiredCapabilities)
_WD_NewTab($sSession)
_WD_Navigate($sSession, "https://app.teamleader.eu/tickets.php")

; Create new ticket
_ChromeClickButtonId($sSession, 'loginButton')
Sleep(5000)
_ChromeClickButtonClass($sSession, '_3F4D5 _3FpQy _1ff4h _2Yigy _1-T6- e6RGs _31W-e')
Sleep(2000)
_ChromeSetInputValueById($sSession, 'name', "TEST")
_ChromeSetInputValueById($sSession,'id_corc_id', ClipGet())
Sleep(3000)

$sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='suggestions_corc_id']/a[@id='sg_corc_id_0']")
$title = _WD_ElementAction($sSession, $sLink, 'attribute', 'title')

If $title == "-3" Then 
    MsgBox(0, "Helaas", "Deze klant staat niet in het systeem! Ga alsjeblieft zelf verder met invoeren!", 100)
Else
    _ChromeClickDropdown($sSession, 'sg_corc_id_0')
EndIf

_ChromeClickSaveButton($sSession, 'thebutton')
Sleep(3000)
_ChromeClickDescription($sSession, 'ahoy-button-base ahoy-button-icon ahoy-button-medium icon-only icon-24x24-edit-outline')

_WD_Shutdown()

Func SetupChrome()
    _WD_Option('Driver', 'chromedriver.exe')
    _WD_Option('Port', 9515)
    _WD_Option('DriverParams', '--log-path="' & @ScriptDir & '\chrome.log"')

    $sDesiredCapabilities = '{"capabilities": {"alwaysMatch": {"goog:chromeOptions": {"debuggerAddress": "localhost:9222"} {"w3c": true, "args":["start-maximized","disable-infobars"]}}}}'
EndFunc

Func _ChromeSetInputValueById($sSession,$Id,$Value)
 $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='"&$Id&"']")
 _WD_ElementAction($sSession,$sButton,'value', $Value)
EndFunc

Func _ChromeClickButtonId($sSession,$Id)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickSaveButton($sSession,$Id)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickButtonClass($sSession,$class)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[@class='"&$class&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickDropdown($sSession, $Id)
    $sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='suggestions_corc_id']/a[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sLink, 'click')
EndFunc

Func _ChromeClickDescription($sSession, $class)
    $sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='description-info']/div[@class='ahoy-widget-header  has-padding']/div[@class='ahoy-widget-actions']/button[@class='"&$class&"']")
    _WD_ElementAction($sSession, $sLink, 'click')
EndFunc

 

Link to comment
Share on other sites

5 minutes ago, Paulliev said:

@Danp2 Yes that works! The idea is as following. I want to go to /tickets.php without logging in. Is it possible that the user is logged in in the current Chrome session and that I can run my script without logging in?

The script should run once you fix the issue in your $sDesiredCapabilities string.

Unsure if there's a way to make it work without logging in. That would likely be determined by the browser's user profile in use. Normally, a webdriver session creates a brand new temporary user profile for that particular session. If you are attaching to a previously launched browser instance, then it should use whatever profile the browser was directed to use when it was first launched.

P.S. Check the wiki for additional details on how to utilize an existing user profile

 

Link to comment
Share on other sites

Hello again,  I tried to use ShellExecute to open a new tab in Chrome in the current Chrome session. That works perfectly! It opens a new tab in my Chrome where I'm already logged into teamleader. But my issue is that it won't run my clicks etc. I think this is because I don't use that Session anymore. Can I fix this and still using ShellExecute? See my code below: 

#include "wd_core.au3"
#include "wd_helper.au3"

$_WD_DEBUG = $_WD_DEBUG_None
Local $sDesiredCapabilities, $sSession

; ShellExecute("chrome.exe", "--remote-debugging-port=9222")
ShellExecute("chrome.exe","https://app.teamleader.eu/tickets.php", @SW_MAXIMIZE)

SetupChrome()
_WD_Startup()

; Create session and log in
$sSession = _WD_CreateSession($sDesiredCapabilities)
; _WD_NewTab($sSession, "https://app.teamleader.eu/tickets.php")
; _WD_Navigate($sSession, "https://app.teamleader.eu/tickets.php")

; Create new ticket
; _ChromeClickButtonId($sSession, 'loginButton')
Sleep(5000)
_ChromeClickButtonClass($sSession, '_3F4D5 _3FpQy _1ff4h _2Yigy _1-T6- e6RGs _31W-e')
Sleep(2000)
_ChromeSetInputValueById($sSession, 'name', "TEST")
_ChromeSetInputValueById($sSession,'id_corc_id', ClipGet())
Sleep(3000)

$sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='suggestions_corc_id']/a[@id='sg_corc_id_0']")
$title = _WD_ElementAction($sSession, $sLink, 'attribute', 'title')

If $title == "-3" Then 
    MsgBox(0, "Helaas", "Deze klant staat niet in het systeem! Ga alsjeblieft zelf verder met invoeren!", 100)
Else
    _ChromeClickDropdown($sSession, 'sg_corc_id_0')
EndIf

_ChromeClickSaveButton($sSession, 'thebutton')
Sleep(3000)
_ChromeClickDescription($sSession, 'ahoy-button-base ahoy-button-icon ahoy-button-medium icon-only icon-24x24-edit-outline')

_WD_Shutdown()

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, "args":["start-maximized","disable-infobars"]}}}}'
EndFunc

Func _ChromeSetInputValueById($sSession,$Id,$Value)
 $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//input[@id='"&$Id&"']")
 _WD_ElementAction($sSession,$sButton,'value', $Value)
EndFunc

Func _ChromeClickButtonId($sSession,$Id)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickSaveButton($sSession,$Id)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//a[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickButtonClass($sSession,$class)
    $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//button[@class='"&$class&"']")
    _WD_ElementAction($sSession, $sButton, 'click')
EndFunc

Func _ChromeClickDropdown($sSession, $Id)
    $sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='suggestions_corc_id']/a[@id='"&$Id&"']")
    _WD_ElementAction($sSession, $sLink, 'click')
EndFunc

Func _ChromeClickDescription($sSession, $class)
    $sLink = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='description-info']/div[@class='ahoy-widget-header  has-padding']/div[@class='ahoy-widget-actions']/button[@class='"&$class&"']")
    _WD_ElementAction($sSession, $sLink,

 

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...