Unable to find element in a form class or div class - selenium

I am unable to find an element in a form class or div class:
//Tried this clicking on Source name.
driver.findElement(By.id("__BVID__62")).click();
//Typing into Source name.
driver.findElement(By.id("__BVID__63")).sendKeys(new String[]{"CNN"});
I tried this:
driver.findElement(By.xpath("//input[contains(#class='form-control') and type='text']")).sendKeys("CNN");
HTML code:
<div class="card-body">
<fieldset id="_BVID__62" role="group" aria-labelledby="__BVID__62__BV_label" class="b-form-group form-group">
<legend id="_BVID__62__BV_label" class="col-form-label pt-0">Source Name</legend>
<div role="group" aria-labelledby="_BVID__62__BV_label" class="">
<input id="__BVID__63" type="text" class="form-control">

use this code :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement input = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='card-body']/descendant::div/input[contains(#id,'BVID')]")));
input.sendKeys("CNN");

As per the HTML you have shared the desired element is having a dynamic ID. So to send text to the <input> node you have to induce WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and starts-with(#id,'__BVID__') and #type='text']"))).sendKeys("CNN");

Related

how to ignore span with List<WebElement>

I am trying to extract Main Text1 and Main Text2 from below DOM structure.
<div class="origination">
<div class="origin">
<h3>
Main Text1
<span class="info">Test1</span>
</h3>
<div class="test">
</div>
</div>
<div class="origin">
<h3>
Main Text2
<span class="info">Test2</span>
</h3>
<div class="test">
</div>
</div>
</div>
I have used below xpath with text() to identify header without span
#FindBy(xpath = ".//*[#id='origination']/div[*]/h3/text()")
List<WebElement> content;
I am trying to print header element without span using below code
for (WebElement element: content){
System.out.println("Header" + element.getText());
}
But I am getting error:
org.openqa.selenium.NoSuchElementException: Timed out after 15 seconds. List elements not found
However, when I used the same XPath on the firepath elements get highlighted in the DOM structure but not on to the page.
Here is the answer.
spanText = element.findElement(By.xpath("//span[#class='info']")).getText()
element.getText().Replace(spanText, string.Empty);

Cannot click on the element ..not able to identify it

<div class="navpage-header-content">
<form action="textsearch.do" role="search" method="GET" class="form-inline navpage-global-search ng-non-bindable" aria-label="Global Search" target="gsft_main">
<input name="sysparm_ck" id="sysparm_ck" type="hidden" value="052ab09a0fa90700fa38563be1050e0fea31866160e7a5e6e0fc925775df282f070903d6"><div class="input-group-transparent">
<input name="sysparm_search" id="sysparm_search" placeholder="Search" type="search" class="form-control form-control-search">
<label for="sysparm_search" title="" data-original-title="Search">
<span class="input-group-addon-transparent icon-search sysparm-search-icon"></span>
</label></div></form></div>
This is the HTML tag for the element.
WebElement ele1 = driver.findElement(By.xpath("//span[#class='input-group-addon-transparent icon-search sysparm-search-icon']"));
ele1.click();
But my script cannot locate the element and click it.
I tried actions , Java script executor , but I just cannot Click on the element.
Actually you are trying to locate the span, You should locate input tag to click on search textbox, it should be like this:
WebElement SearchBox=driver.findElement(By.id("sysparm_search"));
SearchBox.click();
You can also use the xpaths to locate it:
//*[#id='sysparm_search']
//input[#id='sysparm_search']
//input[#name='sysparm_search']
Use this code for xpath:
WebElement SearchBox=driver.findElement(By.xpath("//*[#id='sysparm_search']"));
SearchBox.click();
You can also use name to locate it
Use this code for name:
WebElement SearchBox=driver.findElement(By.name("sysparm_search"));
SearchBox.click();

Getting value of the element

I need to get the value of data-id in the HTML code below.
My attempted code is:
//p[contains(.,'Smart card')]/following-sibling::button[#data-id='633597015500043521']"));
My HTML code shown below:
<form class="select-card-form" novalidate="" method="post">
<input type="hidden" value="SmartCardSelect_3ca61d51-e601-40de-80fd-308bc47b52c6" name="FormName">
<input type="hidden" value="d79cf158-93ad-4c77-b4bc-516ce8b28302" name="CardId">
<div class="select-item ">
<p>Smart card 1</p>
<button class="submit-btn uniform-button button-smaller button-orange select-address" data-id="633597015500043521">
<span>Select</span>
</button>
</div>
</form>
Solution for this (JAVA):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select-item > button"))));
String value = element.getAttribute("data-id");
Java code snippet
WebElement element = driver.findElement(By.xpath("//form[#class='select-card-form']//div[#class='select-item ']//button"));
element.getAttribute("data-id");
Hope this helps.

How to automate the bootstrap fileupload upload control using webdriver

I want to automate the file uploading process which is using a file upload control built in bootstrap.
I am doing the same using webdriver.
Below is my code, but unfortunately it is not working:
element=driver.findElement(By.xpath("//[#id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);
It is giving an element not visible error.
Here is the example of the bootstrap fileupload control which I am trying to automate-
Via JavaScript:
on this URL http://markusslima.github.io/bootstrap-filestyle/
Please see below style-
$(":file").filestyle({icon: false});
Ok. i think i solved it.
WebElement fileInput = driver.findElement(By.id("document"));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("document"));
js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
element);
fileInput.sendKeys(fileName);
the bootstrap-filestyle.js hide a input element so you have to move it to the visible area and then set it in the standard way.
so much trouble for such a simple solution.
Here is my original html code:
<span id="documentUpload">
<input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
<div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
<input type="text" class="input-xlarge" disabled="" autocomplete="off">
<label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
</div>
</span>

I am trying Selenium Webdriver to find input element by class name

How to find element input class="Test_type"? I successfully got by id=Test1 but failing to find element by input class="Test_type"
I have tried:
WebElement mainform = driver.findElement(By.className("mainform"));
List<WebElement> postadchildone2 = mainform.findElements(By.className("formrow"))
for(WebElement formrow: postadchildone2)
{
WebElement formfield = formrow.findElement(By.className("formfield"));
WebElement inputclass = formfield.findElement(By.className("Test_type"))
}
Also tried using CSS selector & XPath, however still it says didn't found, because of the same class name it is not finding input class.
Under for loop I tried doing
formrow.getText()
It displays all the text results but it is not taking class name? I am stuck please help
HTML:
<div class="mainform">
<div class="formrow">
<div class="formrow">
<div class="formrow">
<div class="formfield">
<div id="Test1" class="city_select">
<div class="blank1"/>
<div class="formrow " style="margin-left:10px;">
<div class="formrow">
<div class="formlabel">
<strong>You Are</strong>
</div>
<div class="formfield">
<label>
<input class="Test_type required" type="radio"/>
YouareTest
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
as you have the following piece of code:
<div class="formfield">
<label>
<input class="Test_type required" type="radio"/>
YouareTest
</label>
</div>
We need to outline input element somehow.
So I recommend to locate this element using CSS selector because it is the fastest way.
So 'input' tag has following class attribute= "Test_type required" , that means that to this element 2 CSS classes are applied:
Test_type
required
When locating with CSS selector element, an element B with class attribute y will be notated as B.y, an element with 2 class attributes y and z will be notated as B.y.z then.
So in terms of our task it will look like
input.Test_type.required
But also you can notice that you have another element - div with class attribute =formfield as parent element.
So to locate child element to parent with CSS in the structure like:
<parent class="zz">
<child 1>
<child2> abaracadabra</child>
</child 1>
</parent>
parent.zz child2 => in this way
So the second part of our selector be like :
div.formfield as it be parent selector in relation to input.
So solution combo =
WebElement inputt=driver.findElement(By.cssSelector('div.formfield input.Test_type.required'));
Please try this one, and tell whether it works for you or not.
Since you are trying to fetch with only partial class name, try using the below code
WebElement inputclass = formfield.findElement(By.CssSelector("Input[class*='Test_type'"))