Click OK in popup using Selenium VBA - vba

Trying to automate an on-line order process. Once the order specification is completed you press the Save button. If the item specification (height) is >1000mm a popup is displayed. I am having problems automating Clicking it. It is not in an iframe.
On the second line of code below:
bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click
'Clicking 'causes' the popup warning to display if the >1000mm condition is TRUE. It has only one button - 'OK' (coded as nbsp;OKnbsp; ). Clicking manually allows automatic progress to go to the next page but I need help to get the coding to do this automatically. At the moment the code appears to be hung displaying the popup. Debug indicates that the next instruction cannot be executed - obviously because that findElementById....Click cannot be found because the process has got stuck.
I have tried several Alternatives - please see the code for the most likely that I tried.
If ThisWorkbook.Sheets("SquareSpace").Cells(r, "F").Value > 1000 Then ' The item is more than 1000mm high
bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click
'Alternative 1
bot.FindElementByXPath("//input[#ID='popup_ok' and text()=' OK ']").Click
'Alternative 2
bot.SwitchToAlert.Accept
bot.Wait 1000
Alternative 3
bot.FindElementById("popup_ok").Click
Else ' for when the item is not more than 1000mm high
bot.FindElementById("ctl00_mainContent_ucOrderDetailBlind_btnSave").Click
bot.Wait 500
End If
'Then the next instruction of the form bot.FindElementById ......Click
The HTML with some stuff removed for clarity (text and styling):
<div id="popup_container" class="ui-draggable" style="position: absolute; z-index: 99999; padding: 0px; margin: 0px; min-width: 604px; max-width: 604px; top: 0px; left: 647.5px;"><h1>Warning</h1>
<div id="popup_content" class="alert">
<div id="popup_message"><br>WARNING TEXT</div>
<div id="popup_panel"><input type="button" value=" OK " id="popup_ok"></div>
</div>
</div>
Any suggestions are appreciated. Thanks.

To click on the element with value as OK you can use either of the following Locator Strategies:
Using FindElementByCss():
bot.Wait 5000
bot.FindElementByCss("div#popup_container div#popup_panel > input#popup_ok").Click
Using FindElementByXPath():
bot.Wait 5000
bot.FindElementByXPath("//div[#id='popup_container']//div[#id='popup_panel']/input[#id='popup_ok']").Click

Try using javascript to click the button:
[edit - updated identifier]
element=bot.FindElementByXPath("//input[#id='popup_ok']")
x =bot.ExcuteScript("arguments[0].click();", element)
[code above updated - see comments]
[new below]
Had a bit of fun getting this to work as expected.
This is my little test function to give you an idea of what i used to check things:
Function selenium()
Dim driver As New WebDriver
Dim element As WebElement
driver.Start "chrome"
driver.Get "https://www.google.co.uk"
driver.FindElementByXPath("//input[#name='q']").SendKeys "hello world"
Set element = driver.FindElementByXPath("//input[#value='Google Search']")
x = driver.ExecuteScript("arguments[0].click();", element)
End Function
Not sure why i had to assign a response back from the execute script but it wouldn't work without it.

The best and simplest method I've found is to just use driver.SwitchToAlert(5).Accept. The 5 is just a number of seconds to wait before accepting the error message. You can use any number of seconds in here.

Related

How to click an individual part of 'canvas' in selenium?

I am trying to click a specific part i.e first part(19.9%) of image but its always clicking any of the value which are after 14.3%. Please check the image.[![https://i.stack.imgur.com/WoUlM.png]]
The code is:
<div class="tvimagesContainer" style="width: 80px; height: 241px;">
<canvas class="tabCanvas tab-widget" width="71" height="213" style="display: block; width: 80px; height: 237px;"></canvas>
</div>
I tried with following:
#FindBy(xpath="//*[#id='tabZoneId21']/div/div/div/div[1]/div[5]/div[1]/canvas")
WebElement wbcanvas;
Actions builder = new Actions(driver);
Action drawAction = builder.moveToElement(wbcanvas, 213/12,10).//
.click()
.build();
drawAction.perform();
In place of 213/12,10 put different values and tried but its clicking every time on the middle part of image.
Any help will be appreciated.
Please try it like following, this will click the one above of the middle. You can change the value(213/12). If you want to click below fields then you need to change y to value below 0 :
Actions actions = new Actions(driver);
actions.moveToElement(wbcanvas).moveByOffset(0, (213/12)).click().build();
actions.perform();

Can I get specific "dl" i.e. at First or Second index out of many identified by a CSS Selector in my App

I'm trying to automate a functionality using selenium in my Application Chrome browser. It's an SVG graph based page and shows details upon mouse over it. And this is identifiable with a CSS selector which is returning more than one matching elements(i.e. 6-7 dl , these dls has few child tags then internally containing the values I need to verify -as attached), now my need to select them one by one at a time and verify text of them(which displayed on mouse over).
I got to know on google how to read nth-child from dl but not getting a way to select particular dl at first place.
For example-
my selector is: .d3-tip.n>dl
if I use -.d3-tip.n>dl>dt:nth-child(odd): its giving me all the attributes of dt.. ie 6 values but I nedd values only from fst dl.
Similarly.d3-tip.n>dl>dd:nth-child(even) returning the 6 values of respected dds..
In Actual my app has only one dl (on UI) but don't know why it displaying 6 in DOM...
Plz refer attachment and HTML for a clear understanding of DOM
<div class="d3-tip n" style="position: absolute; top: 44.5px; opacity: 0; pointer-events: none; box-sizing: border-box; left: 515px;">
<dl style="width:335px">
<dt>Space Name:</dt>
<dd>Space</dd>
<dt>Property Type:</dt>
<dd>Office</dd>
<dt>Quoted Area:</dt>
<dd>444 sf</dd>
<dt>Space Usage:</dt>
<dd>Business Park,Commercial School</dd>
<dt>Space Status:</dt>
<dd>For Lease</dd>
<dt>Possession Status:</dt>
<dd>Vacant</dd>
</dl>
<span class="d3-tip__pin"/>
</div>
<div class="d3-tip n" style="position: absolute; top: 44.5px; opacity: 0; pointer-events: none; box-sizing: border-box; left: 515px;">
<dl style="width:335px">
<dt>Space Name:</dt>
<dd>Space</dd>
<dt>Property Type:</dt>
<dd>Office</dd>
<dt>Quoted Area:</dt>
<dd>444 sf</dd>
<dt>Space Usage:</dt>
<dd>Business Park,Commercial School</dd>
<dt>Space Status:</dt>
<dd>For Lease</dd>
<dt>Possession Status:</dt>
<dd>Vacant</dd>
</dl>
<span class="d3-tip__pin"/>
</div>
<--! and so on up to 6 blocks of dl
nth-child is to find the nth-child of any immediate parent element. In your HTML DOM, dd is single child element of each div.d3-tip element. The repetitive child is actually your div.d3tip for it immediate parent element
So your selector has to be written as below to get the first set of dd,
div.d3-tip:nth-child(1)>dl>dd
Getting the second selector also works. This is most important while writing css selector. The second nth has to work. :).
Ok, so I am not sure which of the elements you want...
So... here is a snip that should give you help.
A) with hovering.
B) with looping through the elements.
C) Bonus learn about contains() functionality of XPath...
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
url = "http://your_url"
path_to_chromedriver = "C:\path_to_chromedriver"
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--start-maximized")
browser = webdriver.Chrome(executable_path=path_to_chromedriver,
chrome_options=chrome_options)
browser.get(url)
# list_of_dt_elements_to_hover = browser.find_elements_by_xpath("//div[contains(#class,'d3-tip')]//dl/dt")
list_of_dt_elements_to_hover = browser.find_elements_by_xpath("//div[class='d3-tip n']//dl/dt")
list_of_dd_elements_to_hover = browser.find_elements_by_xpath("//div[contains(#class,'d3-tip')]//dl/dd")
hover = ActionChains(browser).move_to_element(list_of_dt_elements_to_hover[0])
hover.perform()
for dd_ele in list_of_dt_elements_to_hover:
hover = ActionChains(browser).move_to_element(dd_ele)
hover.perform()
print(dd_ele.text)
for dd_ele in list_of_dd_elements_to_hover:
hover = ActionChains(browser).move_to_element(dd_ele)
hover.perform()
print(dd_ele.text)
I hope you find this helpful!

How to click on the element as per the html through VBScript and Selenium

I'm using VbScript with Selenium driver to connect to Chrome (store.steampowered.com/app/681530/NOISZ/) and want to click the "Next in Queue" button which is a div class, but nothing happens. I know I'm on the right track because the below lines does work as expected:
(Displays the text Next in queue)
msgbox(driver.FindElementsByXPath("//div[#class='next_in_queue_area']/div[#class='btn_next_in_queue btn_next_in_queue_trigger']").Item(1).text)
(Scrolls to the Next in queue button)
driver.FindElementsByXPath("//div[#class='next_in_queue_area']").Item(1).ScrollIntoView
(But trying these ones does nothing and also no error messages)
driver.FindElementsByXPath("//div[#class='queue_actions_ctn']").Item(1).Click
driver.FindElementsByClass("next_in_queue_content").Item(1).Click
Using Chrome's Inspect when highligtning this code the Next button is highlighted entirely:
<div class="btn_next_in_queue btn_next_in_queue_trigger" data-tooltip-text="Remove this product from your queue and continue to the next item.">
<div class="next_in_queue_content">
<span>Next in Queue<br>
<span class="queue_sub_text">(11 remaining )</span>
</span>
</div>
</div>
So it is just a question of finding the correct div or span to click
To click the button with text as Next in Queue you can use either of the following solution:
FindElement with XPath:
driver.FindElementByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Click
FindElements with XPath:
driver.FindElementsByXPath("//div[#class='btn_next_in_queue btn_next_in_queue_trigger']/div[#class='next_in_queue_content']/span").Item(1).Click
As an alternative you can use the ExecuteScript() method as follows:
Dim js As IJavaScriptExecutor = TryCast(driver, IJavaScriptExecutor)
js.ExecuteScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))
Or
Imports OpenQA.Selenium.Support.Extensions
driver.ExecuteJavaScript("arguments[0].click();", driver.find_element_by_xpath("//span[starts-with(#id, 'button-')][#class='x-btn-inner x-btn-inner-center']"))

Selenium: Click on element in div container doesn't work

Fyi: I'm using the selenium package for R, the selection code is equal to javascript or python so I'm asking a general selenium question.
I have a container which I have to make visible by a click, this works.
Then I try to select an element inside this container, I think I find it correctly but the click on the element only makes the popup window disappear.
Example code:
<div class="dateRanges" style="top: 275.313px; display: block;">
<a class="top dateOption CUSTOM" id="id32" href="javascript:;">
Benutzerdefinierter Zeitraum
</a>
<a class="dateOption TODAY" id="id33" href="javascript:;">
Heute
</a>
...
</div>
Element I try to find is "top dateOption CUSTOM".
My different tries which all failed:
remDr$findElement(using = 'xpath','//a[contains(#class,"top")]')$clickElement()
remDr$findElement(value = '//a[#class = "top dateOption CUSTOM"]')$clickElement()
remDr$findElements(using = 'css selector','a[class="top dateOption CUSTOM"]')[[1]]$clickElement()
I don't get any error message, the click seems to happen as the popup disappears, but the effect of the button does not.
I tried to save the object, wait a few seconds and click afterwards with no different effect.
I also tried different approaches with "idc4" with the same outcome.
Would appreciate any help, thank you.

element not visible: Element is not currently visible and may not be manipulated - Selenium webdriver

Following is the html
<div id="form1:customertype" class="ui-selectonemenu ui-widget ui-state-default ui-corner-all ui-state-hover" style="width: 165px;">
<div class="ui-helper-hidden-accessible">
<select id="form1:customertype_input" name="form1:customertype_input" tabindex="-1">
<option value="S">Staff</option>
<option value="C">Customer</option>
<option value="N">New To Bank</option></select></div>
<div class="ui-helper-hidden-accessible"><input id="form1:customertype_focus" name="form1:customertype_focus" type="text" readonly="readonly"></div>
<label id="form1:customertype_label" class="ui-selectonemenu-label ui-inputfield ui-corner-all" style="width: 149px;">Staff</label>
<div class="ui-selectonemenu-trigger ui-state-default ui-corner-right ui-state-hover"><span class="ui-icon ui-icon-triangle-1-s ui-c"></span></div></div>
The stylesheet of class="ui-helper-hidden-accessible" is
ui-helper-hidden-accessible {
border: 0;
clip: rect(0 0 0 0);
height: 0px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 0px;
}
Following is my code
WebElement customerType = driver.findElement(By.id("form1:customertype_input"));
Select select = new Select(customerType);
select.selectByVisibleText("New To Bank");
When I try to select "New to Bank" from the dropdown I get exception
element not visible: Element is not currently visible and may not be manipulated - Selenium webdriver
I have tried WebDriverWait technique but of no use, any ideas ?
I don't believe the text for that option is actually visible before you attempt to select it. Try selecting by value instead.
WebElement customerType = driver.findElement(By.id("form1:customertype_input"));
Select select = new Select(customerType);
select.selectByValue("N");
You may need to actually click the selector before being able to select an option, though.
WebElement customerType = driver.findElement(By.id("form1:customertype_input"));
new WebDriverWait(driver, 15).until(
ExpectedConditions.elementToBeClickable(customerType));
customerType.click();
Select select = new Select(customerType);
select.selectByValue("N");
try performing click on customerType before you create object of Select
Well, I found a work around to solve my problem but I am not happy with this. anyways what I did is focused on the div element that controls the dropdown by clicking it and and then sending down arrows keys twice followed by enter key. This selects my desired option. I used the following method
driver.switchTo().activeElement()
I also had the same problem and after hours I realized the browser was trying to click in a element before the page load.
So I create a sleep to solved the problem:
sleep(1)
P.S. - This is a solution that I really don't like.
I'm just pointing you the reason for that.
The best you can do is to check the page that you have the problem and try to optimize the load time.
I encounter the same problem. I have tried many methods.
Finally, the following python code solved the error.
I use javascript code to make the element visible before selecting the option.
css = 'select#state' # css selector of the element
js = """const data_options = Array.from(document.querySelectorAll('{css}'));
data_options.forEach(a=>{{a.style='display:block;';}});""".format(css=css)
self.driver.execute_script(js)
Maybe it's helpful for you!