Selenium WebDriver - Unable to close select drop down menu in Chrome on Mac OS X - macos

I have been Working with Selenium WebDriver for a few months now and I have a problem with a drop down menu within a web app that I am working on.
What is happening is that the test is opening the page, verifying several elements on the page by finding them and then ensuring they are displayed.
After doing that there is some text entered into different fields, then the option select box is clicked on to open the drop down menu.
Following this the test iterates through all the options in the drop down menu until it finds the one it needs, then clicks on that option.
At this point the option is selected but the drop down menu is not closed.
I have tried clicking on the option select again but this has no effect, during the rest of the test other pages are navigated to and the menu does not close.
Then the page is saved and then navigated away from.
However the drop down menu remains until the browser is closed.
This is the code from the app:
<select id="options" name="options" class="options">
<option value="option1 (auto)">option1 (auto)</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>

the first solution I would try is to click on menu options in different ways. Selenium API provides us with this possibility.
1) locate e.g. css selectors of the elements.
String cssOption1 = "select[id='options']>option[value='option1 (auto)']";
String cssOption2 = "select[id='options']>option[value='option2']";
String cssOption3 = "select[id='options']>option[value='option3']";
Also don't forget to verify that you found elements properly e.g .in firepath, firebug addon in ffox:
approach 1
driver.findElement(By.cssSelector(cssOption2)).click();
approach 2 using actions builder API
WebElement mnuOptionElement;
mnuOptionElement = driver.findElement(By.cssSelector(cssOption2));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuOptionElement).click();
more info about Actions builder you can get here
approach 3 using jsExecutor to click on web element. Always works for me in all situations.
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssOption2+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
Hope this works for you

I have solved the problem with a work around, as this is the only way that I have found to work.
Firstly thank you eugene.polschikov for your answer although it didn't solve the problem it did open my eye somewhat, I had no knowledge of action builder, and it has given me some great ideas about future tests.
Also thank you to anyone who read this and pondered over a possible solution.
The workaround that is now in place is that the select is not opened.
The way the code works is that it would open the list and find the one it wanted and click on it, at this point the select wouldn't close, so now the code no longer opens the select in the first place, it clicks on the hidden option to select it, not 100% what i wanted, but it works.
Happy Programming,
Ben.

If a human can press Escape to exit the combobox, you can do that in Selenium by switching to the active element:
from selenium.webdriver.common.keys import Keys
element = driver.switch_to.active_element
element.send_keys(Keys.ESCAPE)

Related

UFT doesn't click a Radio button based on the adjacent text

I am using UFT 15.0.2 to automatize some Web GUI Tests using VB Script. I have to click on a Radio Button which is adjacent to a text (product code) but it doesn't work because when I used Record to click on the WebCheckBox, UFT pasted that line:
Browser("B").Page("P").Frame("FrameID").WebElement("offRadio").Click
Since this html page is under a Shadow-Root, none of the xpath/css is not working. Hence I am trying to use object repository to identify the radio button using the product code like how we write in xpath.
Before shadowDOM implementation the code was working as below with xpath:
CheckBoxInputparam1 = "//span[text()='"
CheckBoxInputparam2 = "']/../../../../../../../../..//PAPER-CHECKBOX"
CheckBoxInputparam3 = "']/../../../../../../../../..//PAPER-RADIO-BUTTON"
checkboxXpath = CheckBoxInputparam1&ProductList(0)&CheckBoxInputparam2
radioBtnXpath = CheckBoxInputparam1&ProductList(0)&CheckBoxInputparam3
Could someone help me?

How do I let Selenium driver click on link which is dynamically created through JavaScript

I am trying to automate actions and unable to select an element due to its dynamic nature.
I am running Selenium web driver on ruby and am trying to select value that is not present in page source.
<a class="linkOtherBrowser" onclick="addChangeStatusField('InitialSelectionPage');submitFormByAction('ChangeStep');return false;" href="#"><div class="processBarElement noSelected">
<div class="whiteBeforeProcessBarTitles"></div>Initial Selection</div>
<div class="endOfElementOfProcessBar"></div></a>
I am trying to select value "Initial Selection" from above.
Could anyone pls help out?
Thanks,
Abhishek
As the HTML is generated by Javascript, You need to inspect the DOM instead of viewsource and write the element locator code accordingly.
Note: In IE, Firefox or Chrome you can press F12 key to see the developer tools and use the inspect element option to check the DOM.
Whatever element is generated dynamically is added in your DOM. WebDriver has capability of clicking on elements are the visible on UI and hence if the generated element is visible to regular user's you can click on the element easily.
To do so, you need to identify the best selector for that newly generated click, could be xpath or css. Once you identify the selector you can consider clicking clicking using following code
WebElement element = driver.findElement(By.xpath("//a[#title='NAME_TITLE']"));
element.click();
OR
WebElement element = driver.findElement(By.css("a[title='NAME_TITLE']"));
element.click();
There are more options within your By.class on picking the element in best way

Selenium webdriver can't find button

EDIT:
I have cleaned this up a bit.
I have a button that looks like this:
<input id="applyRuleButton" class="Button" name="filtersContainer:applyRuleButton"
value="Apply" onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id256', '?wicket:interface=:23:form:filtersContainer:applyRuleButton:
:IActivePageBehaviorListener:0:&wicket:ignoreIfNotActive=true',
'filtersContainer:applyRuleButton' ,function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this),function() { ;wicketHide('applyRuleButton--
ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id256')}.bind(this));;; return false;" type="submit">
Firebug:
<input id="applyRuleButton" class="Button" type="submit"
onclick="wicketShow('applyRuleButton--ajax-indicator');var
wcall=wicketSubmitFormById('id2ee',
'?wicket:interface=:29:form:filtersContainer:applyRuleButton::IActivePageBehaviorListener:0
:&wicket:ignoreIfNotActive=true', 'filtersContainer:applyRuleButton' ,function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this),function() {
;wicketHide('applyRuleButton--ajax-indicator');}.bind(this), function() {return
Wicket.$$(this)&&Wicket.$$('id2ee')}.bind(this));;; return false;" value="Apply"
name="filtersContainer:applyRuleButton">
I'm trying to click it and have tried pretty much everything for 2 days, webdriver does not find the element, IDE does find it:
//This was my first approach, it should work.
It works in IDE, but not Webdriver:
driver.findElement(By.id("applyRuleButton")).click();
//then perhaps this should do the trick, hint: It doesn't:
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
JavascriptExecutor executor3 = (JavascriptExecutor)driver;
executor3.executeScript("arguments[0].click();", element3);
Ok, Id not working, I get it.
Then this should work at least:
driver.findElement(By.xpath("//table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input[#id='applyRuleButton']")).click();
It feels like I am missing something obvious here, some help please?
Additional information:
I have added a 5 second wait, the page is completely loaded.
This button is located in a table:
The Xpath is
/html/body/div[4]/div[2]/form/div[3]/div/div/table/tbody/tr/td/div/div/table/tbody/tr[6]/td/input
Webdriver error, no matter what I throw at it, is: Unable to locate element
I have used both 'click' and 'submit', still no success.
I think in this case there are two possibilities :
Either there is another element having same id/xpath.
OR Element present in another iframe.
Is the button visible. Selenium click (latest firefox 26 and latest webdriver 2.39.0) does not sometimes implicitly scroll; Or it may not scroll it fully. So scroll it into view - Scroll Element into View with Selenium and then it should work.
Note Selenium Best Practise try to use By.Id,By.CSSSelector and if nothing gets use By.Xpath in the order of priority. ( Use the FireFinder, FireBug plugin to test XPath or CSS)
This might be a synchronization issue. Such issues can be solved using smart waits.
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementExists((By.Id("applyRuleButton"))));
WebElement element3 = driver.findElement(By.id("applyRuleButton"));
And that should work perfectly fine.
There is absolutely nothing wrong with your selector. I just don't think you're invoking the click correctly.
Try:
driver.findElement(By.id("applyRuleButton")).click();
If this doesn't work, then you might have to invoke a WebDriverWait since you have this question marked as [ajax]
Could you post the entire html?
As a simple experiment, I took the html snippet that you posted and wrote a short python script that invokes selenium:
from selenium import webdriver
br = webdriver.Firefox()
br.get("put your path to snippet here")
button = br.find_element_by_id("applyRuleButton")
print button.get_attribute("name")
button.click()
br.close()
I can find the button and extract the attribute "name" which prints "filtersContainer:applyRuleButton". This is admittedly a very limited experiment, but it suggests that the issue is related to not being where you think you are on the page.
Try this:
driver.findElement(By.Name("filtersContainer:applyRuleButton"));
If this doesn't help, check whether this button is located in any other frame. If so, you may have to find and move your focus to that frame and then locate this button.
First try to identify the button by writting correct xpath using firebug, if you are able to identify button by that xpath then use that xpath while writing your script.
driver.findElement(By.xpath("//input[# type='submit' and # id='applyRuleButton'")).click();
This is ajax application use proper explicit/ webdriver wait till the button gets downloaded
I see that this thread is old, but I was looking at it today (Sept/2021) as I was having the same problem: I could see the name / id/ type of the button, but it would never be found.
I discovered that when I had clicked in a prior link, it opened a new tab in my browser, but Selenium did not change the focus to the new tab, so it couldn't find the ID of the button I was looking for.
I solved it with :
driver.find_element_by_id("export").click() #driver
time.sleep(2)
driver.switch_to.window(driver.window_handles[1]) # Change focus to the new tab
driver.find_element_by_id("0001btn").click() #click
driver.close() #close new tab
switching to a specific frame helped me to resolve the same issue. (python + selenium)
I installed the Selenium Recorder extension on chrome and recorded my steps, and found out that the recorder had a step to select a frame = 0, so adding
self.home_page.driver.switch_to.frame(0)
self.home_page.click_on_element_by_id("clickSubmit")
solved the problem.

Interacting with VB6 client using hidden control in embedded web browser control

I'm having difficulty trapping a programmatically triggered click event on a hidden button control from a ASP.NET MVC 4 web app inside a VB6 thick client (which is using a web browser control). I'm able to trap the click event itself using the following:
Private WithEvents WebDoc As HTMLDocument
Private Function WebDoc_onclick() As Boolean
Select Case WebDoc.activeElement.iD
Case "A"
Do something
Case "C"
Do something else
End Select
WebDoc_onclick = True
End Function
And this works just fine if the control is visible. But if the control is invisible:
<div class="HideBtnDiv">
<input id="C" name="NoItems" type="button" class="BtnDiv" style="display:none"/>
</div>
and I try to trigger a programmatic click via one of the following:
$("#C").('click');
$("#C").trigger('click');
$("#C").triggerhandler("click");
$("#C").focus();
$("#C").trigger('click');
I'm getting an empty string for the "id" attribute and as a result I can't distinguish which button was clicked. This button serves no purpose other than to indicate to the VB6 app that a certain criteria has been met and that's the reason why I need it to be hidden. Does anyone have any idea why the id is getting stripped? Or is there any other way to communicate back to the client?
I've also tried filtering by element style using
Select Case WebDoc.activeElement.Style
Case "display:none"
Do something else
End Select
but it came back as "[Object]" so no luck there either. Please let me know if there is a way around this.
Thanks,
Lijin
You seem to have tried several ways of dynamically triggering the click event, but did you try the most obvious way:
$("#C").click();
???
But here is what I would do:
1- Make all of your buttons visible, by removing "display:none" from their style
2- Wrap the buttons you want to hide in a new DIV
3- Set "display:none" style in the newly created DIV
4- You can then trigger the .click() event of any button even if not visible by calling $(id).click();
Thanks, Ahmad. Actually I meant .click() not .('click'). Sorry about that.
Anyway, I tried your suggestion and made the button visible and set the style of the wrapping div to display:none but the id attribute was still coming through as an empty string.
However, I did figure out another way to get this to work. If I keep the wrapping div and button as visible and then focus and click when the condition is met and then do a hide(), my problem is resolved!
$("#C").focus();
$("#C").trigger('click');
$("#C").hide();
The button doesn't get displayed and VB6 still passes the id on the click event. The weird thing is it requires the focus() call to still be made. Without it, I'm back to square one. Not sure if this is a bug.

Google Apps Script listbox to dropdown box

I'm building a UI in Google Sites using Apps Script and I am able to create a listbox. According to the documentation if you change the setVisibleItemCount to 1 it will become a dropdown box.
I have tried both 1 and 0.
Neither seems to make it a drop-down box. Using Firefox 13.0.1 to view. Have also tried Safari.
Anyone had any luck with this?
Code looks like this:
var vPanel = container.createVerticalPanel();
//List box
var lb = container.createListBox(true).setId('listbox').setName('listbox');
// add items to ListBox
for(var i=0;i<LIST_OF_PROJECTS.length;i++){
lb.addItem(LIST_OF_PROJECTS[i]);
}
vPanel.add(lb);
lb.setVisibleItemCount(1); //supposed to make it a drop-down but doesn't
lb.setSelectedIndex(0);
This is all inside a Google Site and the page that is being displayed is a Apps Script Page. Perhaps you are NOT using Google Sites? Above code gives me a single line but no drop down arrow.
Could you post your relevant code please ?, It's working for me on firefox (slightly differently), chrome & safari. (see screen cap when I click the selector, SetVisibleItemCount is 1)
thx
EDIT : One important point : to get the list acting as a dropdown list you have to choose 'disable multiple selection', in other words : createListBox(false) or no parameter... but not 'true' as it is in your code !! (now we know why it doesn't work in your case ;-)
With this parameter set to false , it works as expected in standalone webapp, embedded on site and linked to spreadsheet without any difference.
Don't call:
setVisibleItemCount
at all.

Resources