Selenium - How to click OK button in a pop up using text containing in that button - selenium

The code below is shown in firebug for the OK button in the popup of my application
<button id="ext-gen219" class="x-btn-text" type="button">
OK
</button>
Here the button id is randomly generated and also the position of ok button is not identifiable. Is there a way to click on the button by its text? Like the button contains a text called OK.

For Selenium IDE or Selenium RC, xpath=//button[contains(., 'OK')] ought to be a good locator.

I am going on the premise that the text of the popup boxes are not identical so I would recommend something like this:
List<IWebElement> dialogBox = driver.FindElements(By.Class("dialogBoxClass");
int buttonIndex = dialogBox.FindIndex(i => i.FindElement(By.Class("DisplayedTextElementClass").Text.Contains("some unique text here"));
dialogBox[buttonIndex].FindElement(By.ClassName("x-btn-text")).Click();
This captures the parent element of all the dialog boxes and then parses each object to find the index of the box you want and then clicks on the 'Ok' button in the correct object.

In case of a submit button this locator css=input:submit[value="OK"] should work. In case of a button element take css=button:contains("OK").

Related

robotframework- unable to click a button on a modal dialogue box

when I open a particular webpage it has a EULA form that appears on top of the home page in the form of a modal in HTML/CSS. I have tried the XPath of the accept button but it says element not found also tried using handle alert.
is there a unique way to handle a dialogue box in robotframework?
image of HTML in inspect view
SeleniumLibrary.Set Focus To Element ${button_on_popup_page}
SeleniumLibrary.Click Element ${button_on_popup_page}
I figured it out, shown in the above code. I first set focus to the button and then used the click element. This seemed to work.

selenium, hidden button by overlapped input ( not clickable)

Is there any way on how to click a hidden button by an overlapped input text field, for example if you go to www.google.com and enter a text to search for, selenium can not find the "Search Google" button because it is hidden by the autocomplete of the text field.
Thanks.
You can bypass the validation by using #click! instead of #click. Basically, this triggers the click via JavaScript rather than through standard Selenium commands.
browser = Watir::Browser.new
browser.goto('www.google.com')
browser.text_field(name: 'q').set('watir')
browser.button(name: 'btnK').click!
If you are only using Selenium, you can do:
btn = driver.find_element(name: 'btnK')
driver.execute_script('arguments[0].click();', btn)
As discussed in the comments, you could also close the suggestions box before trying to click the button. You can do this by moving focus to any other element - eg the first link on the page. Depending on what you're testing, this may or may not have value.
browser.text_field(name: 'q').set('watir')
browser.link.focus # move focus to any other element so suggestions close
browser.button(name: 'btnK').click

Finding buttons on a webpage using Selenium

I am trying to automate a web based application and I am having trouble finding buttons in order to click on them.
Basically there are a number of text boxes on a screen and next to each text box is a "Go" button. Once you hit the "Go" a popup will be overlayed on the screen saying "Are you Sure?" and 2 buttons Yes/No. Where I am getting stuck is after the first set of buttons on the Yes/No popup buttons. Each button has a non-unique class name and no name variable, so I am finding each button by its tagName and then clicking on the relevant element
WebElement aButtons = driver.findElements(By.tagName("button");
aButtons .get(x).click();
This works until aButtons .get(x).click(); throws an exception saying the element isn't clickable. On further inspection of aButtons it has a size of 78 elements, despite there being only 33 visable buttons on the page.
Does anyone have a suggestion on how to find my buttons better? Or perhaps how to handle the exception such that I can, with a bit of trial and error, find the element? If the exception could be ignored, I could probably use a for loop in increments of 10 in order to find which element the Yes/No buttons correspond to.
If I ruled the world I would go to the developer of this webpage and ask them to make the buttons more easily identifiable, but that unfortunately is out of the question
When I inspect the elements in question this is the block in the html that gets highlighted
"Go" button:
<
button type "submit" ng-click="vm.click()" ng-disabled = "vm.isDisabled()"
class = "btn btn"> Generate <button/
>
<
button class ="btn" ng-show="vm.runScripts" ng-click="vm.runScript()" ng-
disabled ="vm.runScript.isScriptRunning">
>span class = "glyphicon play" aria-hidden="true"></span>
Yes
</button>
I was having trouble with the formatting of the above since I am unfamiliar with posting html code.v the 2nd button is the yes button.
(Note the "No" button has the same "btn" class name). When I tried to find the element based off of the class name " glyphicon play" I got an error saying compound class names are not valid. I was able to find the "Go" buttons since they were sequential, so like aButtons .get(1).click(); was the first Go button on the page, aButtons .get(2).click(); was the second etc.
I cannot share the url since it is an internal site.

Wait for asyncronous event with Selenium IDE

I have two radio buttons and one text box. Every time I switch the radio button the text box is cleared.
Following sequence:
text box is empty
I switch the radio button (Selenium IDE: click -> radioButton)
I input some text in the text box (Selenium IDE: type -> textBox)
Problem:
The input for text box is done before clear of radio button takes place. So everytime I input something in the text box the input is deleted after a short period of time.
How can I avoid this?
Try adding a "Pause" command after clicking on the radio button
Command: pause
Target: 3000 (or as applicable)
Value:
This way your execution will wait for 3 seconds, and hopefully the text box will be cleared when you get to the "SendKeys" step.
PS. Try using webdriver with your language preference.

Webdriver (c#) - find button presumably by text

I'm struggling to get a handle on a button. Would anyone be able to point me in the right direction on how to get a handle on the button AND click on it using xpath or a css selector? Here is the code:
<button class="trans-button mtrn-dialog-button" title="" value="">OK</button>
I have tried to get it by text. It seems to work fine (in that it can find the element) but it doesn't seem to want to provide the click event to the button
Using the following Xpath expression (Inner Text) should allow you
click the button
IWebELement okButton = aDriver.FindElement(By.XPath("//button[.='OK']"));