Need unique field to identify using Selenium WebDriver - selenium

I need to find the the unique field to identify the search box and enter text in it.
<style type="text/css">
</head>
<body style="min-height: 81px;">
<div class="shared-page" data-cid="view221" data-view="views/shared/Page" data-render-time="0.02">
<a class="navSkip" tabindex="1" href="#navSkip">Screen reader users, click here to skip the navigation bar</a>
<header role="banner">
<a id="navSkip"></a>
<div class="main-section-body" role="main">
<div class="timeline" data-cid="view4947" data-view="views/timeline/Master" data-render-time="0.107">
<div class="section-padded section-header">
<div class="timeline-title" data-cid="view5085" data-view="views/timeline/Title" data-render-time="0.001">
<div class="search-bar-wrapper shared-searchbar" data-cid="view4948" data-view="views/shared/searchbar/Master" data-render-time="0.104">
<form class="search-form" action="" method="get">
<table class="search-bar search-bar-primary">
<tbody>
<tr>
<td class="search-input" width="100%">
<div id="search" class="shared-searchbar-input" data-cid="view4949" data-view="views/shared/searchbar/Input" data-render-time="0.002">
<div class="search-field-background"> </div>
<div class="search-field-wrapper">
<label class="placeholder-text" for="397271.5897375417" style="display: block;">enter search here...</label>
<textarea id="397271.5897375417" class="search-field" autocapitalize="off" autocorrect="off" spellcheck="false" name="q" rows="1"></textarea>
textarea is the field related to the search box. I tried using class, name but it says unable to locate element, the numeric id too is dynamic. Please let me know what to use in this case.

It should be possible to find this element by class name
in ruby:
driver.find_element(:class, "search-field")
or in java:
driver.findElement(By.className("search-field"));

Following xpath should work:
//div[#id='search']//textarea[#class='search-field']

If the <textarea> is only one, you can search with
driver.findElement(By.tagName("textarea"));

Use xpath,
driver.findElement(By.xpath("//textarea[#class='search-field']"));
Or
driver.findElement(By.name("q"));

Related

Why doesn't the search for an element that does not contain a value work in xpath?

A piece of html page that I have:
<html lang="ru">
<body>
<form name="substitutionFM" class="form-data" onsubmit="return !1">
<input name="message" id="message" type="hidden" value="error.password.invalid">
</form>
<div _ngcontent-c17="" class="content">
<input _ngcontent-c15="" automation-id="bui-primitive-textfield__native-input" class="input bui-autofill ng-untouched ng-pristine ng-valid ng-star-inserted" aria-placeholder="" id="bui_01584205838454" type="text" tabindex="0" inputmode="numeric" x-inputmode="numeric" aria-describedby="bui_01584205838454_described-by" style="padding-left: 16px; padding-right: 16px;">
</div>
</body>
</html>
My xpath that I am trying to find the elements I want:
.//input/#*[(contains(., "otp") or contains(., "ode") or contains(., "assword") or contains(., "numeric")) and not(contains(., "hidden"))]/..
As you can see, I don't want to find hidden elements, but that doesn't work, and both elements are in the browser and in the selenium, but I only need the second one. How can I fix this?
Try this one
'//input[#*[contains(., "otp") or contains(., "ode") or contains(., "assword") or contains(., "numeric")] and not(#*[contains(., "hidden")])]'

Why can I click an input with type=radio of a h:selectOneRadio, but not one of a p:selectOneRadio with Graphene/Selenium?

A h:selectOneRadio results in <input type="radio"> in a table and p:selectOneRadio in <input type="radio"> in a table with some divs around the input. The id for both is [form id]:[selectOneRadio id]:[option number] which I can use successfully for the plain JSF in a Graphene functional test when accessing it with #FindBy(id="[...]") whereas the PrimeFaces variant fails due to org.openqa.selenium.ElementNotInteractableException. Investigating the generated HTML I don't see the difference
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="/34696ceb-eeaa-4b35-88dd-f3c8fc5901bf/javax.faces.resource/theme.css.xhtml?ln=primefaces-aristo">
<link type="text/css" rel="stylesheet" href="/34696ceb-eeaa-4b35-88dd-f3c8fc5901bf/javax.faces.resource/primefaces.css.xhtml;jsessionid=48ca919d0b7e89661f92149ac321?ln=primefaces&v=5.0">
<script type="text/javascript" src="/34696ceb-eeaa-4b35-88dd-f3c8fc5901bf/javax.faces.resource/jquery/jquery.js.xhtml;jsessionid=48ca919d0b7e89661f92149ac321?ln=primefaces&v=5.0"></script>
<script type="text/javascript" src="/34696ceb-eeaa-4b35-88dd-f3c8fc5901bf/javax.faces.resource/primefaces.js.xhtml;jsessionid=48ca919d0b7e89661f92149ac321?ln=primefaces&v=5.0"></script>
<title>Facelet Title</title>
</head>
<body>
<form id="mainForm" name="mainForm" method="post" action="/34696ceb-eeaa-4b35-88dd-f3c8fc5901bf/index.xhtml;jsessionid=48ca919d0b7e89661f92149ac321" enctype="application/x-www-form-urlencoded">
<input name="mainForm" value="mainForm" type="hidden">
<table id="mainForm:mainSelectOneRadio">
<tbody>
<tr>
<td>
<input name="mainForm:mainSelectOneRadio" id="mainForm:mainSelectOneRadio:0" value="a" type="radio">
<label for="mainForm:mainSelectOneRadio:0"> a</label>
</td>
<td>
<input name="mainForm:mainSelectOneRadio" id="mainForm:mainSelectOneRadio:1" value="b" type="radio">
<label for="mainForm:mainSelectOneRadio:1"> b</label>
</td>
<td>
<input name="mainForm:mainSelectOneRadio" id="mainForm:mainSelectOneRadio:2" value="c" type="radio">
<label for="mainForm:mainSelectOneRadio:2"> c</label>
</td>
</tr>
</tbody>
</table>
<table id="mainForm:mainSelectOneRadioPrime" class="ui-selectoneradio ui-widget">
<tbody>
<tr>
<td>
<div class="ui-radiobutton ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="mainForm:mainSelectOneRadioPrime:0" name="mainForm:mainSelectOneRadioPrime" value="aPrime" type="radio">
</div>
<div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default"><span class="ui-radiobutton-icon ui-icon ui-icon-blank"></span>
</div>
</div>
</td>
<td>
<label for="mainForm:mainSelectOneRadioPrime:0">aPrime</label>
</td>
<td>
<div class="ui-radiobutton ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="mainForm:mainSelectOneRadioPrime:1" name="mainForm:mainSelectOneRadioPrime" value="bPrime" type="radio">
</div>
<div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default"><span class="ui-radiobutton-icon ui-icon ui-icon-blank"></span>
</div>
</div>
</td>
<td>
<label for="mainForm:mainSelectOneRadioPrime:1">bPrime</label>
</td>
<td>
<div class="ui-radiobutton ui-widget">
<div class="ui-helper-hidden-accessible">
<input id="mainForm:mainSelectOneRadioPrime:2" name="mainForm:mainSelectOneRadioPrime" value="cPrime" type="radio">
</div>
<div class="ui-radiobutton-box ui-widget ui-corner-all ui-state-default"><span class="ui-radiobutton-icon ui-icon ui-icon-blank"></span>
</div>
</div>
</td>
<td>
<label for="mainForm:mainSelectOneRadioPrime:2">cPrime</label>
</td>
</tr>
</tbody>
</table>
<input name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-485558793831512050:990657069126697889" autocomplete="off" type="hidden">
</form>
</body>
</html>
nor do I if I deploy the application on Payara 4.1.2 or any other reason for the ElementNotInteractableException.
The access is done with
#RunWith(Arquillian.class)
public class MyManagedBeanTest {
private static final String WEBAPP_SRC = "src/main/webapp";
private final static Logger LOGGER = LoggerFactory.getLogger(MyManagedBeanTest.class);
#Deployment(testable = false)
public static Archive<?> createDeployment0() throws TransformerException, XPathExpressionException, ParserConfigurationException, SAXException, IOException {
WebArchive retValue = ShrinkWrap.create(WebArchive.class)
.add(EmptyAsset.INSTANCE, "beans.xml")
.addClasses(MyManagedBean.class)
.addAsWebInfResource(
new StringAsset("<faces-config version=\"2.0\"/>"),
"faces-config.xml");
Maven.configureResolver().workOffline().resolve("richtercloud:graphene-click-input-radio:war:1.0-SNAPSHOT").withoutTransitivity().asList(JavaArchive.class).forEach(dependency -> retValue.addAsLibrary(dependency));
//add all webapp resources
retValue.merge(ShrinkWrap.create(GenericArchive.class)
.as(ExplodedImporter.class)
.importDirectory(WEBAPP_SRC)
.as(GenericArchive.class), "/", Filters.include(".*\\.(xhtml|css|js|png)$"));
ByteArrayOutputStream archiveContentOutputStream = new ByteArrayOutputStream();
retValue.writeTo(archiveContentOutputStream, Formatters.VERBOSE);
LOGGER.info(archiveContentOutputStream.toString());
return retValue;
}
#Drone
private WebDriver browser;
#ArquillianResource
private URL deploymentUrl;
#FindBy(id = "mainForm:mainSelectOneRadio:0")
private WebElement mainSelectOneRadioOption0;
#FindBy(id = "mainForm:mainSelectOneRadioPrime:0")
private WebElement mainSelectOneRadioPrimeOption0;
#Test
public void testAll() {
browser.get(deploymentUrl.toExternalForm()+"index.xhtml");
LOGGER.debug(browser.getPageSource());
mainSelectOneRadioOption0.click();
mainSelectOneRadioPrimeOption0.click();
}
}
I'm searching for a solution which triggers JSF action methods and AJAX listeners!
I'd be interested in a generic approach as well, e.g. p:selectOneButton produces
<div id="mainForm:mainSelectOneButtonPrime" class="ui-selectonebutton ui-buttonset ui-widget ui-corner-all">
<div class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-left">
<input id="mainForm:mainSelectOneButtonPrime:0" name="mainForm:mainSelectOneButtonPrime" value="aPrime" class="ui-helper-hidden" type="radio">
<span class="ui-button-text ui-c">aPrime</span>
</div>
<div class="ui-button ui-widget ui-state-default ui-button-text-only">
<input id="mainForm:mainSelectOneButtonPrime:1" name="mainForm:mainSelectOneButtonPrime" value="bPrime" class="ui-helper-hidden" type="radio">
<span class="ui-button-text ui-c">bPrime</span>
</div>
<div class="ui-button ui-widget ui-state-default ui-button-text-only ui-corner-right">
<input id="mainForm:mainSelectOneButtonPrime:2" name="mainForm:mainSelectOneButtonPrime" value="cPrime" class="ui-helper-hidden" type="radio">
<span class="ui-button-text ui-c">cPrime</span>
</div>
</div>
<input name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="-5130093024933213812:2291815208147638618" autocomplete="off" type="hidden">
which doesn't seem to have anything in common with the HTML generated for p:selectOneRadio at first sight. Maybe there's a trick.
I'm using PrimeFaces 6.1.
ui-helper-hidden-accessible is a JQuery layout helper to hide items visually. The radio button you see is actually the parent element, <div class="ui-radiobutton ui-widget">.
The problem can be resolved by clicking on the radio button label. The 'for` attribute put the focus on the input label associated with it
#FindBy(css = "[for='mainForm:mainSelectOneRadioPrime:0']")
private WebElement mainSelectOneRadioPrimeOption0;
#Guy has the right answer.
The issue here is that PrimeFaces is applying its own styling on top of the HTML which is covering the element you are trying to click.
Selenium checks that the targeted element mainForm:mainSelectOneRadio:0 receives the events when the element on top is clicked. But in this case the overlay is done with a sibling container which is not an descendant of targeted element. Thus Selenium assumes that the element will not receive the events and raises an ElementNotInteractableException (see event bubbling and propagation).
You can clearly see the issue by visiting oneRadio.xhtml and by inspecting the radio button with a right click. You'll see that the selected DOM element and the <input> are located in two different branches of the DOM tree.
To overcome this issue, either click the label since it has no overlay (see solution from #Guy). The label has the for attribute which mean that all the events are forwarded to the element assigned to for which is the targeted <input>.
You could also directly click the overlay. Though, you'll have to use an XPath to express the relationship.
Parent of parent of the targeted <input> :
#FindBy(xpath = "id('mainForm:mainSelectOneRadioPrime:2')/../..")
private WebElement mainSelectOneRadioPrimeOption0;
Or first <td> having the targeted <input> :
#FindBy(xpath = "//td[.//input[#id='mainForm:mainSelectOneRadioPrime:2']]")
private WebElement mainSelectOneRadioPrimeOption0;

Selenium WebDrivers -Not able to select dynamic check box which has no name

<div id="checkboxfield-3844" class="x-field x-form-item x-field-default x-form-cb-checked x-form-dirty" style="width: 492px;">
<label id="checkboxfield-3844-labelEl" class="x-form-item-label x-form-item-label-left" style="margin-right:5px;width:200px;" for="ext-gen6460">Is Stitching Point:</label>
<div id="checkboxfield-3844-bodyEl" class="x-form-item-body x-form-cb-wrap" role="presentation" style="width: 287px;">
<input id="ext-gen6460" class="x-form-field x-form-checkbox" type="button" hidefocus="true" autocomplete="off" aria-checked="true" aria-invalid="false" role="checkbox" aria-describedby="checkboxfield-3844-errorEl" style="-moz-user-select: text;" data-errorqtip="">
</div>
<div id="checkboxfield-3844-errorEl" class="x-form-error-msg" style="display:none"></div>
<div class="x-clear" role="presentation"></div>
</div>
This is my Div , out of it iam not able to select the checkbox since there is no name .Need some help on this
Assuming there is only one 'label tag' with innerHTML/text as "Is Stiching Point:", the below xpath will point to the checkbox:-
driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']")).click();
NOTE: Above is a java code. In case you are using a different language binding, you can refer this to employ the same thing.
Edit
Below is the alternative way using JavascriptExecutor:
WebElement element = driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

Pls how to access message in span tag

<html>
<body>
<section id="click" class="content mCustomScrollbar _mCS_1">
<div id="mCSB_1" class="mCustomScrollBox mCS-dark" style="position:relative; height:100%; overflow:hidden; max-width:100%;">
<div class="mCSB_container" style="position:relative; top:0;">
<form id="registration" action="http://abcd.com/setup/user-registration/index" method="post" name="registration">
<article class="frm-content">
<div class="leftsection">
<div class="form-element">
<fieldset>
<label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname" style="border: 1px solid red;">
<label>
<span class="required">*</span>
First Name:
</label>
<input id="firstname" type="text" maxlength="50" value="" name="firstname" style="border: 1px solid red;">
<span id="error_firstname" class="errormsg" role="alert">Please use only letters,numbers, period, single space and underscore</span>
</fieldset>
</article>
</div>
</section>
</body>
</html>
I Above code I want to find Please use only letters,numbers, period, single space and underscore message. The xpath for that is /html/body/section[2]/div/div/form/article/div/div/span but it is not accessing by gettext() method. Pls how to access this ????
getText() method fetches only visible (i.e. not hidden by CSS) innerText of respective element. Which might be a cause for the problem.
Please try the following to check if element is displayed.
Driver.FindElement(By.id("error_firstname")).isDisplayed();
You can add WebDriverwait till that element is displayed and then fetch the text from that element.
WebDriverWait Driver_Wait=new WebDriverWait(Driver, 10); //10 denotes Total_Secs to wait
Driver_Wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("error_firstname")));
Hope this helps.
By using the id "error_firstname" we can only get the values in span which has id as error_firstname..
So use the id "click" which serves the best

How to click on button "Submit Quote" its id="div_5_1_1-lnk" is dynamically changing in different pages of applications

<div id="Form_Scope1" class="Form_Scope topLevel CoachView CoachView_show" data-eventid="" data-viewid="Form_Scope1" data-config="config1" data-bindingtype="" data-binding="" data-type="com.ibm.bpm.coach.Snapshot_a30ea40f_cb24_4729_a02e_25dc8e12dcab.Form_Scope">
<div id="Action_Group2" class="Action_Group topLevel CoachView CoachView_show" data-eventid="" data-viewid="Action_Group2" data-config="config9" data-bindingtype="" data-binding="" data-type="com.ibm.bpm.coach.Snapshot_a30ea40f_cb24_4729_a02e_25dc8e12dcab.Action_Group">
<div id="div_5_1" class="ContentBox" data-view-managed="true" style="display: none;"> </div>
<div class="s-action-group clearfix sticky">
<div class="l-nodeId" style="">QTO001_N002A</div>
<div class="p-action-group">
<div id="div_5_1_1" class="Action CoachView CoachView_show" data-eventid="boundaryEvent_7" data-viewid="Action3" data-config="config15" data-bindingtype="" data-binding="" data-type="com.ibm.bpm.coach.Snapshot_a30ea40f_cb24_4729_a02e_25dc8e12dcab.Action">
<button id="div_5_1_1-lnk" class="p-primary-btn btn btn-primary" type="button" title="" data-original-title="" style="background-color: rgb(56, 168, 182);">
<i class="icon-indent-right"></i>
<span>Submit quote</span>
</button>
<a href="#action" style="display: none;">
</div>
How to click on button "Submit Quote" its
1.id="div_5_1_1-lnk" = one page of application
2.id ="div_9_1_1-lnk" = Another page of application
is dynamically changing in different pages of applications.
I have tried by xpath expression by position or relative path it is not working please let me know any other option available for this?
The following selector would work,
driver.findElement(By.cssSelector("div.Action button[id$='lnk']"));
Here '$' indicates that the id of button should end with value 'lnk'.
The equals sign in attribute selectors may be prefaced by other characters which alter the meaning a bit.
Remember that classes and ID's are attributes too, and can be used with attribute selectors.
Refer this for more info.
Try driver.findElement(By.cssSelector("button[id^='div_']"));
Click it using the class name
options=driver.find_elements_by_class_name("icon-indent-right")
for option in options:
if(option.text=="Submit quote"):
option.click()