Selenium Find Element by xpath using value as well - selenium

I am trying to check to see if buttons are present on a page. The number of buttons varies depending on the type of account that logs in. The code below returns true, so I know that the xpath is working.
Boolean WorkspacePresent = driver2.findElement(By.xpath("//body/div[2]/div[1]/div[1]/form[1]/section[1]/input[1]")).isEnabled();
However, I need to make sure that the value is also included, since this just checks how many buttons are there. If I wanted to check for a "Workspace" button, could I still use Find By Xpath?

To identify the Workspacebutton button use following xpath.Similar way you can identify other button.
driver2.findElement(By.xpath("//input[#name='Workspacebutton' and value='Workspace']"))

Related

how to handle hyperlinked textarea in selenium

I have a page, where the link is defined with text area. How do I handle this to pass a value to it via selenium?
Here is the UI Code:
<a:TextArea name="Some Name">
I want to click on the link also wants to type the value into it. But with Selenium, I am not able to do that.
Can someone know how to do this?
Nice Question.
How about manipulating the DOM elements with javascript.
As you haven't provided the code so here is the descriptive answer to your question. You need to follow certain steps in order to achieve that.
Find Elements
Disable the link using Javascript, here you can stop its click event
Once you had disabled the click event, you can sendkeys to that field.
Once you done with sendKeys, You can undo the above changes and make click on field.(If You can't undo then just get the href attribute of the <a> tag and store it in any variable and once done with sendkeys, visit that link by passing the variable.
Do let me know if any query occurs.

How can I select always the last radio button?

I have a verification page to test by selenium webdriver automation. On the verification page there are always three questions.
But those questions are selected from a pool of questions and so for a single user different questions might appear every separate time he comes to verification page.
So say there are 20 questions in my pool and there are five options (radio answers) for each question, so there are 100 separate radio buttons and each has its separate id/name in DOM.
I am unable to automate this piece of the webpage.
In order to proceed with my testing, I need to always select the last radio button for each of the three questions.
The last radio always contains either "None" or "never" in the label text and radio text label is clickable.
Also the name locator always starts with "1402248" for each radio button.
I am using Page object model in my projet.
Can someone help me to understand how can I identify each radio webelement?
I am using this:
#FindBy (xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')]")
protected WebElement oVerifyIdentityFirstAnswer;
This CSS selector should find all radio buttons that are last of their type inside a parent element
driver.findElements(By.cssSelector("input[type='radio']:last-of-type"))
If your radio buttons are all encapsuled individually, move the last-of-type to that container element:
driver.findElements(By.cssSelector("a:last-of-type" > input[type='radio']))
This returns a list of WebElement, which you can iterate over and click every entry. I wouldn't use the #FindBy annotation by the way, since it often leads to staleness exceptions.
Try changing your Xpath to:
xpath = "//*[#class='radio']//a[contains(text(),'never') or contains(text(),'None')][last()]"

Get selected checkbox without knowing checkbox name (generated dynamicaly)

I am working on an add-on for the new google sheets and seem to have hit a problem. I am dynamically generating x number of checkboxes for a user to select. When the user selects some boxes and submits, I am submitting this data to a server handler. However, I can't find a way to get out which checkboxes were selected as I don't know their names. The names are dynamically generated and represent of the ID of the content to retrieve for the user.
Is there any way to find out which checkboxes have been checked on the server side when the name of the checkboxes isn't known?
When you are setting the element's name, set the id to the same as the name and ensure each checkBox has a click handler. Then use:
var source = e.parameter.source;
which returns the id of the clicked element.
The examples given by Srik and Serge insas in response to this question should hopefully help you. There are a few similar posts. Just search for "e.parameter.source"
You'll also find info in the docs here.

In a group check box is it possible to select an option by clicking anywhere in the screen and not exactly inside the check box?

From a group check box is it possible to select only one option. And I want to know whether a particular option can be selected without exactly clicking inside the check box. i,e Is it possible to select an option by just clickig somewhere in the screen where the text of the option is displayed.
I was testing an application and i found that it was able to select an option even without clicking inside the check box, so wanted to know whether that is a validation bug!!
I do not think that it is a bug if there is only one radio button or check box on the screen. Such type of functionality is sometimes provided in order to provide extra functionality to the user. There are certain validations that are capable to do this. But if there are multiple check boxes or radio buttons on the screen then it must be a bug. Because in case of multiple check boxes on the screen the script does not know which button to check.

Selenium IDE not recording Dijit Combo Values

I have an application which uses Dijit Combobox. What happens is, I click on the cell. It opens up a dropdown. This is getting properly recorded in the IDE. But, when I choose a value from the drop down, IDE is not recording that part.
Here is the firebug screenshot of the combo box that appears on clicking on the cell. As you can see it has two values cm2 and mat1. I need the IDE code to choose either of these values.
I tried something like
click - //div[#id='csi_table_Widget_5']/div[2]/table/tbody/tr/td[11] This works fine for opening the drop down.
click - //div[#id='widget_csi_widget_FilteringSelect_10_dropdown']/ul/li[4]
This does not work for choosing a value from the drop down.
Any help, please?
If you absolutely have to click the value, then I think you need to check the ID you are using. Based on the DOM you should be using //div[#id='csi_widget_FilteringSelect_10_dropdown']... instead of widget_csi_... I'm not sure a selector on widgetid would work in selenium.
Alternatively, if you know what value you will be entering into the combo, you could use selenium.type for all but the last character you need to enter, and then selenium.keyPress for the last one. This will fire dojo's change method and select the value in question, unless there is more than one matching the string provided.