Getting value of the element - selenium

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.

Related

Unable to find element in a form class or div class

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");

Selenium/Python - Element not reachable by keyboard

I am trying to fill in a form automatically. I have recorded a script with Selenium.
One of the field to populate is the zip code. When I start typing the code, a new window opens to suggest appropriate option (javascript autofill)
I need to select the first item the ul (cf. html below)
I am quite new to Selenium and though I have been reading the Selenium/html documentation I am totally stuck for almost 1 month on this...
Many thanks in advance for your support
My code is as follows and I received the error message "Element is not reachable by keyboard"
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "selected")))
first_option.send_keys(Keys.RETURN)
**HTML**
<div id="localisation_left">
<div class="line toDisable">
<label for="location_p" class="label">Ville ou code postal *</label>
<div class="field-wrapper location-container">
<div class="inputWrapper">
<i id="browserGeoloc" class="icon-geoloc icon-2x blue"></i>
<div class="loaderGif-small hidden"></div>
<input class="nude" name="location_p" id="location_p" autocomplete="off" value="Paris 75010" type="text">
<input name="zipcode" value="" type="hidden">
<input name="city" value="" type="hidden">
<script type="text/javascript">
var numberOfLocation = 1, numberOfAuthorizedLocation = 1;
var cityNewadMultipleLocation = new MultipleLocationNewad('input[name="location_p"]', numberOfLocation, numberOfAuthorizedLocation);
cityNewadMultipleLocation.cityAndZipcodeAreSelected = true;
</script>
<input name="region" value="" type="hidden">
<input name="dpt_code" value="" type="hidden">
</div>
<ul class="location-list visible" style="top: 43px;">
<li data-region="12" data-dpt-code="75" class="selected">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75011</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75015</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75009</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75010</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75017</span>
</li>
You can click on the first option, instead of pressing Enter key
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
condition = EC.visibility_of_element_located((By.CSS,
"label[for='location_p'] + div ul.location-list > li"))
first_option = WebDriverWait(driver, 15).until(condition)
first_option.click()
I had a similar issue, and the above solution did not work for me (it would throw an invalid syntax error).
I first used the find_element_by_css_selector function, which selects the first occurrence of the element with given attributes. This did not work.
Then I used the find_elements_by_css_selector (notice the s), which returns a list of the elements with given attributes. There were two elements in that list. Of course the first one (with index [0]) was not accessible by keyboard: this is equivalent of doing (1) above. But the second element (with index [1]) was accessible by keyboard.
Problem solved.
Try selecting by using Xpath below
elem = driver.find_element_by_id("location_p") elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.Xpath,
".//*[#id='localisation_left']/div/div/ul/li[1]")))
first_option.click()
If anyone faces Element not reachable by keyboard issue, one can also seek below approach:
input_xpath = '//input[#type="file"][#name="files[]"][#class="class_name"]'
input_element = self.driver.find_element_by_xpath(input_xpath)
## to make element visible:
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";',
input_element)
input_element.send_keys('~\Desktop\Release2.pdf')

Not able to identify check box based on the name given beside it using xpath

I wanted to identify the CheckBox based on the name given to it by using the xpath, but was not able to reach till the text uniquely.
The html code is in below. How can I get the dynamic xpath for 'text1' or 'text2' mentioned in the html?
<html>
<body>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text1
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text2
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text3
</div>
</div>
<div class="section-content">
<div>
<input class="cls" type="checkbox"/>
text4
</div>
</div>
</body>
</html>
The below code worked for me
driver.findElement(By.xpath("//div[contains(.,'text4')]/input")).click();
To identify the node with text as text1 you can use the following line of code :
WebElement element_text1 = driver.findElement(By.xpath("//div[#class='section-content']/div[contains(normalize-space(), 'text1')]"));
To get identify the Check Box associated with the node with text as text1 you can use the following line of code :
driver.findElement(By.xpath("//div[#class='section-content']/div[contains(normalize-space(), 'text1')]/input"));
Try this:
WebElement body = dr.findElement(By.xpath("/html/body"));
List<WebElement> div = dr.findElements(By.className("section-content"));
for(int i=1;i<div.size();i++)
{
String checkBoxText = dr.findElement(By.xpath("/html/body/div["+i+"]/div")).getText();
if(checkBoxText.equals("text3"))
{
dr.findElement(By.xpath("/html/body/div["+i+"]/div/input")).click();
break;
}
}

How to select C++ value if any radio button not selected using selenium

How to select C++ value if any radio button not selected
<form name="testform" action="" method="POST">
<div align="center">
<br>
<input type="checkbox" name="option-1" value="Java">Java
<input type="checkbox" name="option-2" value="C++">C++
<input type="checkbox" name="option-3" value="Python">Python
</br>
</div>
We can induce a simple check on the WebElement to check if it is selected or not and if not selected click on it to select as follows:
WebElement element= driver.findElement(By.xpath("//input[#name='option-2']"));
if(!element.isSelected())
{
element.click();
}
You can use this :-
webElement element= driver.findElement(By.xpath("//input[#name='option-2']"));
element.getAttribute("value"))
or
element.getText();

leniuNot able to enter value in text box of form in selenium webdriver

Hi I am using way2sms website and in send sms screen i am not able to enter the mobile number in mobile number field using selenium webdriver.
HTML Code: `
form id="smsFrm" name="smsFrm" method="post">
<input id="ssaction" type="hidden" name="ssaction" value="ss"/>
<input id="Token" type="hidden" name="Token" value="F448FDFD10E9288F9B4A204EF40EB29A.w803"/>
<div id="smilebox" style=" display:none;">
<div class="Sms fl">
<label>Mobile Number</label>
<div class="m91">
<span>+91</span>
<input id="mobile" type="text" onchange="javascript:dispLocMob(this);" onkeydown="javascript:dispLocMob(this);" onkeyup="javascript:dispLocMob(this);" value="" maxlength="10" placeholder="Mobile Number" name="mobile"/>
</div>`
Selenium Code :
`obj.findElement(By.xpath("//*[#id='sendSMS']/a")).click();
Thread.sleep(5000);
//obj.findElement(By.id("mobile")).sendKeys("8186867724");
obj.findElement(By.xpath("//*[#id='mobile']")).sendKeys("1234567890");`
Your element has an id, which is always the best way to address an HTML element using Selenium - very simple but still absolutely unique (bc otherwise it wouldn't be valid HTML). So try:
findElement(By.id("username"))
Full C# sample (should be similar in Java):
static void Main()
{
var driver = new ChromeDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(15));
driver.Navigate().GoToUrl("http://site23.way2sms.com/content/index.html");
IWebElement element = driver.FindElement(By.Id("username"));
element.SendKeys("000");
}
I switched to the frame and got it detected.
driver.switchTo().frame(driver.findElement(By.id("frame")));
driver.findElement(By.id("mobile")).sendKeys("123456");