So I need to test website pigu.lt. I need to change my personal data. Here is my code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class TestSelenium {
public static void main(String[] args){
WebDriver driver=new ChromeDriver();
driver.get("https://pigu.lt/ru/u/login");
driver.manage().window().maximize();
WebElement element=driver.findElement(By.name("email"));
element.sendKeys("qwertyuiop600309#gmail.com");
WebElement password=driver.findElement(By.name("password"));
password.sendKeys("Qwerty");
driver.findElement(By.name("login")).click();
driver.findElement(By.xpath("//a[#href='u/info']")).click();
driver.findElement(By.name("name")).sendKeys("Qwerty");
driver.findElement(By.name("surname")).sendKeys("Qwerty");
driver.findElement(By.xpath("//input[#id='option1']")).click();
}
}
And here is the html of the website:
<div class="form-row">
<label>Пол</label>
<div class="pill-swicher">
<input type="radio" value="m" name="sex" id="option1">
<label for="option1">Мужчина</label>
<input type="radio" value="f" name="sex" id="option2">
<label for="option2">Женщина</label>
<input type="radio" value="n" name="sex" checked="" id="option3">
<label for="option3">Не хочу говорить</label>
</div>
</div>
But when I try to select the gender from radio button, it shows that element is not intractable.
The input element is not clickable, you have to click the label element for that input.
This will do the trick:
driver.findElement(By.xpath("//label[#for='option1']")).click();
Related
In this website
how to select the international telephone input with flags and dial codes dropdown in selenium, for cellphone field. The selenium code is not selecting
Code I wrote.
<div class="control-group ">
<label class="control-label required" for="CellPhone">Cell Phone</label>
<div class="controls">
<input class="form-control phone-number" data-val="true" data-val-intltel="Invalid number" data-val-required="The Cell Phone field is required." id="CellPhone" name="CellPhone" type="text" value="" />
<span class="help-block">
<span class="field-validation-valid" data-valmsg-for="CellPhone" data-valmsg-replace="true"></span>
</span>
</div>
</div>
package testing123;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class dancekarwebsite1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Vpatiballa\\Desktop\\Lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://dancekar.com/parent-center/register");
driver.manage().window().maximize();
//*[#id="Email"]
//Actions actions = new Actions(driver);
// Scroll Down using Actions class
// actions.keyDown(Keys.CONTROL).sendKeys(Keys.END).perform();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,250)", "");
//driver.findElement(By.xpath("//div[#class='selected-flag']")).click();
Actions action = new Actions(driver);
WebElement optionsList = driver.findElement(By.xpath("//div[#class='selected-flag']"));
action.moveToElement(optionsList);
List<WebElement> options = driver.findElements(By.xpath("//div[#class='selected-flag']"));
for(WebElement option : options) {
if (option.getText().equals("United Kingdom +44")) {
option.click();
}
}
}
}
You should click on your flag to open dropdown, you use action.moveToElement but that doesn't perfom a click and doesn't open dropdown. Try:
action.moveToElement(optionsList).build().perform();
You may need wait till dropdown is opened.
To get list of elements use:
List<WebElement> options = driver.findElements(By.cssSelector(".country .country-name"));
Given the following object:
<div class="myinput_login">
<label class="item item-input myinput_login_1">
<i class="icon ion-android-contact placeholder-icon"></i>
<input class="sn ng-pristine ng-valid ng-empty ng-touched" type="text" placeholder="Please enter the inverter SN" ng-model="remoteLoginData.uuid" style="">
</label>
</div>
I am trying to automate the input of my Serial Number into the object. I've already tried to find the element by name, id, class_name, but I always get a "no such element" error.
So the questions are:
What element type should I be searching by?
What is the value of the object name I should be searching for? "sn", "sn ng-pristine ng-valid ng-empty ng-touched", or?
Thanks.
Try one of these two
box= WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input[#placeholder="Please enter the inverter SN"]')))
box=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input.sn.ng-pristine.ng-valid.ng-empty.ng-touched')))
box.click()
box.send_keys('Serial Number')
Import
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Are you just looking for an xpath to use?
//div[contains(#class,'myinput_login')]//input
I was trying to select the element from the list. With the below code it figures out the correct element. But it doesn't click on the element. Not getting how to proceed.
HTML Code
<div id="__test0-defaultXML--idQueueName" data-sap-ui="__test0-defaultXML--idQueueName" style="width: 100%; max-width: 100%;" class="sapMSlt sapMSltDefault sapMSltHoverable sapMSltMinWidth sapMSltWithArrow" aria-labelledby="__label7 __test0-defaultXML--idQueueName-label" role="combobox" aria-disabled="false" aria-expanded="false" tabindex="0">
<input id="__test0-defaultXML--idQueueName-hiddenInput" aria-multiline="false" aria-readonly="true" tabindex="-1" class="sapUiPseudoInvisibleText">
<label id="__test0-defaultXML--idQueueName-label" for="__test0-defaultXML--idQueueName" class="sapMSltLabel" aria-live="polite">- Select -</label>
<span id="__test0-defaultXML--idQueueName-arrow" class="sapMSltArrow"></span>
<ul style="width:100%;max-width:100%" class="sapMSelectList" role="listbox">
<li tabindex="0" class="sapMSelectListItem sapMSelectListItemBase sapMSelectListItemBaseHoverable sapMSelectListItemBaseSelected" role="option" aria-selected="true" aria-setsize="4" aria-posinset="1">- Select -</li>
<li tabindex="0" class="sapMSelectListItem sapMSelectListItemBase sapMSelectListItemBaseHoverable" role="option" aria-selected="false" aria-setsize="4" aria-posinset="2">sap/smtest/test/a1</li>
<li tabindex="0" class="sapMSelectListItem sapMSelectListItemBase sapMSelectListItemBaseHoverable" role="option" aria-selected="false" aria-setsize="4" aria-posinset="3">sap/smtest/test/test</li>
<li tabindex="0" class="sapMSelectListItem sapMSelectListItemBase sapMSelectListItemBaseHoverable" role="option" aria-selected="false" aria-setsize="4" aria-posinset="4">sap/smtest/test/tt</li>
</ul>
</div>
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
public boolean selectn() {
boolean result = false;
int attempts = 0;
while(attempts < 1) {
try {
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement list1 = driver.findElement(By.xpath("//*[#id=\"__test0-defaultXML--idQueueName\"]/ul"));
list1.click();
List<WebElement> Listnw = list1.findElements(By.tagName("li"));
for(WebElement li : Listnw) {
if (li.getText().equals("sap/smtest/test/a1")) {
li.click();
}
}
result = true;
break;
} catch(StaleElementReferenceException e){
}
attempts++;
}
return result;
}
After WebElement list1 = driver.findElement(By.xpath("//*[#id=\"__test0-defaultXML--idQueueName\"]/ul"));.
Please add list1.click(); and see if it works.
Leave the code below as it is.
I am new to selenium, tried different methods to click the Login button in a webpage, but ended up with "Unable to locate element: " error. Appreciate your help on how i can do it correctly.
<form class="LoginForm__Form-gmPGuK fIaXrM" autocomplete="off">
<div class="InputWrapper__Wrapper-dYpPUc btujOC">
<label class="Input__Container-evMrUq XWErm"><span class="Input__Label-dTgnUu ikGhjC">Username</span><input class="Input-eXLwAq eqVwiS" name="username" type="text" maxlength="20" autocomplete="off" value="" style="font-size: 1.6rem;"></label>
<p class="InputWrapper__ErrorMessage-bFHpkO jmYdPT">Invalid username</p>
</div>
<div class="Password__Wrap-kEDFHF gajvNg">
<div class="InputWrapper__Wrapper-dYpPUc btujOC"><label class="Input__Container-evMrUq ipuRfT"><span class="Input__Label-dTgnUu ikGhjC">Password</span><input class="Input-eXLwAq eqVwiS" name="password" autocapitalize="off" id="password" type="text" maxlength="20" autocomplete="off" value="" style="font-size: 1.6rem;"></label></div>
</div>
<div class="LoginForm__Agree-djIMue hEHJaH">By logging in, you agree to our <a class="LoginForm__Terms-diivNK iYiAkF" href="https://www.EEE.com/yyyyyyy/rr/terms-and-conditions.html" target="_blank">Terms & Conditions</a></div>
<button class="Button-hYXUXp bTvZVR" type="submit">LOG IN</button>
</form>
Try this code with WebdriverWait
WebElement username= driver.findElement(By.name("username"));
username.sendKeys("dsfs");
WebElement password= driver.findElement(By.name("password"));
password.sendKeys("fsdfdsfs");
WebElement LogInbtn= driver.findElement(By.xpath("//button[text()='LOG IN']"));
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(LogInbtn));
LogInbtn.click();
If it is in frame then first go to frame and then use above code
My html form contains multipl html tags such <form>
My html file: myFile.html
<body>
<div class="globalContainer">
<div class="test1" id="formM" width="848" height="720" method="post" name="devis" onreset="return vider();">
<div class="content-1" id="cadreGlob">
<div id="contentForm">
<div class="preview">
<div class="left_col">
<fieldset id="haut">
<label class="labelForm" id="labelPriorite">label1:</label>
<select id="selectPrio">
<option value="labT">subLabel11</option>
<option value="labP">subLabel12</option>
</select><p></p>
<form enctype="multipart/form-data" method="post">
<label class="labelForm" id="labelFile1">label2:</label>
<input id="upload1" type="file" name="file[]" />
</form>
</fieldset>
</div>
<div class="right_col">
<fieldset id="haut">
<form name="page">
<label class="labelForm" for="cb" id="labelPopulation">lebel3:</label>
<input type="checkbox" id="cb" name="cb" checked="checked" onclick="valid();showPop();" /><br />
<label class="labelForm" for="ta" id="labelMessage2">label4:</label>
<textarea disabled="true" id="ta" name="ta" cols="22" rows="9"></textarea>
<label class="labelForm" id="labelFile2" >label5:</label>
<input id="upload2" type="file" name="valider" id="butonParc" disabled="disabled"/>
</form>
</fieldset>
</div>
<div class="left_col">
<p></p>
<fieldset id="bas">
<label class="labelForm" id="labelServiceOp">label6:</label>
<select id="selectServOp">
<option value="def">subLabel61</option>
<option value="sec">subLabel62</option>
</select><p></p>
</fieldset>
</div>
<div class="right_col">
<p></p>
<fieldset id="bas">
<label class="labelForm" id="labelAdressage" >label7:</label>
<select name="ToutePopD" id="ToutePopD">
<option value="toujours">subLabel71</option>
<option value="parfois">subLabl72</option>
</select>
<select name="ToutePopA" id="ToutePopA">
<option value="toujours">subLabl73</option>
<option value="parfois">subLabel74</option>
</select>
</fieldset>
</div>
<div class="right_col"><p></p>
<form action="submit.html" id="sub" name="formValid">
<input type="submit" id="validation" value="validate" name="submit" />
</form> <p></p>
<script>
function vider()
{
document.getElementById("formM").value = "";
return false;
};
</script>
<input type="reset" id="cancel" value="Cancel"name="reset" />
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My first problem
when i execute this command
...
...
this.driver.findElement(By.id("validation")).click();
...
...
My test is not switch to the url : submit.html
My submit.html
<body>
<h2><center>Form validation with Succee</center></h2>
</body>
My second problem:
When i execute the following code, i have an error : Unable to locate element : {"method":"id","selector":"sub"}
public class SeleniumTest {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private final StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
final Properties properties = System.getProperties();
this.baseUrl = properties.getProperty("base.url", "myIp:8080/project");
}
#Test
public void firefoxTest() throws Exception {
this.driver = new FirefoxDriver();
testSelenium();
verifyValidation(this.baseUrl +"submit.html");
}
private void testSelenium() throws Exception {
this.driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
this.driver.get(this.baseUrl + "myFile.html");
new Select(this.driver.findElement(By.id("formM")).findElement(By.xpath("//fieldset[#id='haut']/select[1]"))).selectByVisibleText("subLabel11");
this.driver.findElement(By.id("upload1")).sendKeys("myUrl\\myFile.txt");
this.driver.findElement(By.id("validation")).click();
}
private void verifyValidation(String urlValidation) {
String submit =
this.driver.findElement(By.id("sub")).getAttribute("action");
if (submit == urlValidation) {assertEquals("url problem : ", submit, urlValidation);
}
#After
public void tearDown() throws Exception {
this.driver.quit();
final String verificationErrorString = this.verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(final By by) {
try {
this.driver.findElement(by);
return true;
} catch (final NoSuchElementException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
final Alert alert = this.driver.switchTo().alert();
final String alertText = alert.getText();
if (this.acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
this.acceptNextAlert = true;
}
}
}
I don't know why WebDriver can not find my id ! ?
Thanks for help !
For your 2nd issue,
it comes into my mind, when there is no submit.html file on your server, page
not found displays. Then, on that page, there is no "id" element -> you get
NoSuchElement Exception throws
There are 4 issues in your scripts
1 - Your xpath //fieldset[#id='haut']/select[0] is not correct, xpath starts from [1]
2 - There is no label "subLabel11", only "subLabl11" exists
3 - this.driver.findElement(By.id("sub")).getAttribute("action"); will return *full-url* (In this case myIp:8080/project/submit.html
4 - if (submit == urlValidation) will always return False since they are 2 objects in different memory location.
Suggest you change it to if (submit.equals(urlValidation))
Beside these above notes, I run your script well on my machine and there is no issue. Here's my script:
new Select(browser.getBrowserCore().findElement(By.id("formM")).findElement(By.xpath("//fieldset[#id='haut']/select"))).selectByVisibleText("subLabl11");
browser.getBrowserCore().findElement(By.id("upload1")).sendKeys("myUrl\\myFile.txt");
browser.getBrowserCore().findElement(By.id("validation")).click();
String submit =
browser.getBrowserCore().findElement(By.id("sub")).getAttribute("action");
System.out.println("submit: "+ submit);
if (!submit.equals("submit.html")) {
System.out.println("comparator: FALSE");
};
Here's the test result
2014-07-21 18:13:44 [main]-[INFO] Started Browser
2014-07-21 18:13:44 [main]-[INFO] Pause 500 ms
2014-07-21 18:13:45 [main]-[INFO] Opened url: http://myIP:8305/
submit: http://myIP:8305/submit.html
comparator: FALSE
2014-07-21 18:13:51 [main]-[INFO] Pause 500 ms
2014-07-21 18:13:51 [main]-[INFO] Quitted Browser
PASSED: stackOverFlowTest
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
For your first question, the below code work fine with me.
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("Your URL");
WebElement validate=driver.findElement(By.id("validation"));
validate.click();
driver.close();
}
Following is the code for your 2nd question:
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("Your URL");
WebElement element=driver.findElement(By.id("selectPrio"));
Select sel=new Select(element);
sel.selectByVisibleText("subLabel12");
driver.close();
}
Thank you all for tracks you give me.
I finally succeeded to solve my problem
For my first problem:
I had a mistake in the URL of my page submit.html
For my second problem:
when I execute the command: this.driver.findElement this.driver.findElement(By.id("validation")).click();
the page myFile.html where there is my id "sub" is not supported by the driver but submit.html
And then I made the switch back to my original page myfile.html and I have no error.
Thank you again!