Behat to click on element by class or ID name? - behat

Is it possible for behat to find an element by class name to click on? It looks like only the following is searched for: id|name|title|alt|value
For example, how could you successfully identify this element to click on?
<a class="button medium round signup" href="http://link.com" data-reveal-id="signupModal">sometext</a>
Also here is a simple page with a button, that has an ID. How come the following does not access the button?
<button id="myBtn" type="button" onclick="myFunction()">Try it</button>
Given I am on "http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_button_form"
When I press "myBtn"
Thanks

The button can not be accessed because it is contained in an iframe, you need to switch to that iframe to make the button available.
You can create steps to click on an element by any type of identifier (class included), or you can implement new step that requires css/xpath selectors.
Here you can find an example of a method to click by selector.

The first HTML code is nothing but a HREF. Behat should be easily able to identify it by using the line below:
Given I follow "sometext"
If the above doesn't work, you can simply use the CSS selector and click the element:
$locator = '.';
$this->getSession()->getPage()->find('css',$locator)->click();

Related

How to find webelement when there is no ID in Selenium test script

I have a button displayed on the browser with the following tag which doesn't have ID or a single CSS class name. How can I find the Webelement and add a click to this button.
<button aria-describedby="notes" aria-label="Notes" autofocus="" class="btn btn-primary" role="dialog" type="button">OK</button>
Web elements can be located by XPath or CSS Selectors based on any unique combination of any attribute values or / and tag names and also based on their parent or child elements.
Since you didn't share the entire HTML of that page we can only guess about the correct unique locator for that element.
With XPath it can be something like:
"//button[text()='OK']"
Or
"//button[#aria-label='Notes']"
In CSS Selector style it will be
"button[aria-label='Notes']"
Consider locating the Element by Tag Name. Go to the parent element which has a id or class name and get subelements by tag name (parentelement.find_element_by_tag_name('button')) which is your button
Besides that, the button in your example has two classes.
You click on a button via the .click() command.

Unable to derive Xpath syntax for radio buttons without any distinguished value inside div tag

I want to click a radio button out of 10 radio buttons on a webpage but each radio button tag is exactly same inside Div/INPUT tag, i.e for each radio button values are exactly same and hence not able to derive a xpath to click on it. Can't use contains text to levarage radio button name as radio button name is in different SPAN tag so can't use that as a reference, Please help me: below is the code :
<div class="classname">
<input name="category.value" type="radio" class="classname">
</input>
<span class="classname">Radio button Name</span>
driver.find_element_by_xpath("//span[#class='classname']/input[#value=1]").click()
Your HTML must have some kind of return type, maybe value or name. I belive you can use that.
Mentioned in the comments can you please try:
//span[text()='Radio button Name']/preceding-sibling::input[1]
Not that this is case sensitive.
This works based on the html provided:
If it doesn't work for you can you please verify your HTML. There are lots of other ways of getting the element. If you need to modify the html just do it and let us know :-)

Not able to select first element using css selector

I have a requirement where HTML elements have following structure.
<div class="user">
Name
Logout
</div>
I want to click on first element using css selector.
I am using Behat 3 for automation.
You can do it in multiple ways, one of them is to use the existing step:
I follow "Name"
Another way is to find the element and use the click() on it, or to create a custom step that clicks on the element by selector, for example using css selector I click ".user a"

Press a button on a website using VBA

I am trying to write a code to be able to press a button on a website using VBA. This is the element I get by inspecting the website :
button class="btn btn-cta xs-liquid btn-progress pull-right l-margin-15-bottom" type="submit">verder.
There is no id for the element, I only get the class and type. How can I use these to be able to write an efficient code?
Thank you
If the page is static (it does not change), then you can use GetElementsByTagName or GetElementsByClassName, and take the element with the zero-based index i from that collection: .Document.GetElementsByClassName("btn btn-cta xs-liquid btn-progress pull-right l-margin-15-bottom")(i).
In case of more complicated scenarios I would recommend XPath or CSS search.

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver

Can't click to Customize link when div+link together with FakeAnchor class using selenium web driver
In my Ajax application, we have dropdown + link (Customize) together in div and i want to click to Customize link. I have locator and which was working fine for Customize link with old selenium but it doesn't with latest web driver. Can anyone please point me the problem or suggest something to make it work?
Expected:
Clicking to Customize link should open respected option (it actually opens dialog).
Actual:
Below locator clicks to dropdown button instead of Customize link due to such a complex page DOM which has no actual href or anchor tag.
Locator:
css=div[id$='_repeatDesc'][class='FakeAnchor']
Html:
<div id="zcs1_repeatDesc" class="FakeAnchor" style="cursor: pointer;">Customize</div>
Code:
webDriver().findElement(By.cssSelector("div[id$='_repeatDesc'][class='FakeAnchor']")).click();
I think your locator is not unique, may be it is locating dropdown element that's why it clicks to dropdown button instead of Customize link.
You should try using By.xpath() with text() node to locate this element as below :-
webDriver().findElement(By.xpath(".//div[text() = 'Customize']")).click();
Or As I'm seeing in HTML element has id attribute, if it's unique I'd to locate desire element and it's not being changed dynamically, you can try also using By.id() as below :-
webDriver().findElement(By.id("zcs1_repeatDesc")).click();
Edited :- If you want to click using JavascriptExecutor try as below :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webDriver().findElement(By.xpath(".//div[text() = 'Customize']")));