I am new to selenium automation and while learning radio button handling in selenium, I tried to automate language setting in amazon.in page. I am unable to select the radio button listed in the page.
site: https://www.amazon.in/customer-preferences/edit?ie=UTF8&preferencesReturnUrl=%2F&ref_=topnav_lang
code used: driver.findElement(By.xpath("//input[#value = 'ta_IN']")).click(); (I tried to select the language tamil- TA (3rd radio button)). Can anyone guide me in this?
public class seleniumTutorial2 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.in/customer-preferences/edit?ie=UTF8&preferencesReturnUrl=%2F&ref_=topnav_lang");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[#value = 'ta_IN']")).click();
}
}
'''
try using this xpath://span[text() = 'TA']
driver.findElement(By.xpath("//span[text() = 'TA']")).click();
Maybe try to click on //input[#value = 'ta_IN']/following-sibling::i
Related
I've been spending more than an hour to find similar cases but nothing helpful. I am new to Java and Selenium and only one member who develops the Selenium Automation script at the moment.
Okay, the target system's HTML code structure is as below.
What I did to click on the element on the topframe is, for example, (simplified snippet);
Page page = new Page();
page.setTopFrame();
page.a_button.click();
class Page{
#FindBy(id="a_button")
public WebElement a_button;
public void setTopFrame() {
driver.switchTo().frame("topframe");
}
}
But when I run this codes it returns "org.openqa.selenium.NoSuchElementException" Exception.
I don't know what I am missing now. Any advice will be thankful.
--Edited--
Since I post this one, other Exception found which is "NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null.
The constructor in Page class should initialised with driver from main Test class. Be like.
Page page = new Page(driver);
And Page class has
class Page{
WebDriver driver;
public Page (WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
...
}
Maybe you are currently in another frame. So try:
public void setTopFrame() {
driver.switchTo().defaultContent();
driver.switchTo().frame("topframe");
}
Page class{
WebDriver driver;
public Page (WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
...
}
Thank you all read my question.
I hope this answer help someone in trouble with same issue.
I am trying to upload the file in selenium using java. When I run my test case then it passed every time but the file is not uploading. See below site and my code where I performing.
Site URL: https://files.fm/
Xpath where i want to upload: //input[#name='file_upload[]']
Note: If this xpath is incorrect then please update in comment.
Code:
#BeforeTest
public void OpenBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://files.fm/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void FileUpload() throws InterruptedException, IOException {
Thread.sleep(2000);
WebElement file = driver.findElement(By.xpath("//input[#name='file_upload[]']"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
}
use below code :
WebElement file = driver.findElement(By.xpath("//input[#id='file_upload']//following-sibling::input"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
WebElement startUploadButton = driver.findElement(By.xpath("//div[#id='savefiles']//div"));
startUploadButton.click();
Hope that helps you:)
You choosed the wrong input. I tried to send to the other input and it worked
Here is xpath of it.
String inPath = "//*[#id='uploadifive-file_upload']/input[2]";
I have problem with testing my app which is intended for online shopping. I noticed that all buttons from application working but I have problem with system buttons(look at the picture). I’m using to recognize the button UiAutomatorViewer.
At the picture I’m trying add product to cart but when I’m click button “Dodaj”(means Add) the window closes but there are any actions in application, the product should be added to cart. I tested this application manually and everything work correct.
I tried to operate the button in many ways but still nothing. Does anyone know how to solve it? It’s very important functions to testing for me.
public class Product extends MainPage {
private AndroidDriver<AndroidElement> driver;
#FindBy(id = "com.mec.leroy:id/product_add_to_cart")
private WebElement addToCart;
#FindBy(xpath = "//android.widget.Button[#index='1']") //problem with button
private WebElement buttonAdd;
#FindBy(id = "com.mec.leroy:id/product_name")
private WebElement productName;
public Product(AndroidDriver driver) {
super(driver);
this.driver = driver;
}
public Product addProduct() throws InterruptedException {
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"do koszyka\"));");
addToCart.click();
buttonAdd.click();
Thread.sleep(5000);
return new Product(driver);
}
public boolean productName() throws InterruptedException {
Thread.sleep(2000);
try {
productName.getText();
return true;
} catch (ElementNotVisibleException e) {
return false;
}
}
public class Connector {
public AndroidDriver Settings() throws IOException, InterruptedException {
runAppium();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "33004db396a6c2d1"); // nazwa urządzenia
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability("noRest", "true");
capabilities.setCapability("fullReset", "false");
// capabilities.setCapability("useKeystore", "true");
//uruchomienie aplikacji z poziomu telefonu
capabilities.setCapability("appPackage", "com.mec.leroy");
capabilities.setCapability("appActivity", "com.mec.leroy.main.activity.MainActivity");
//inicjalizacja połączenia z Appium Server
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
return driver;
}
You are using UiAutomatorViewer (that uses old UiAutomator), but in your tests you are providing UiAutomator2: important to know that these frameworks build snapshot xml in a different way, so your locator might be incorrect.
I suggest to try inspect and interact with your button using official appium-desktop:
Install & launch it
Run the server via appium-desktop
Set exact the same capabilities you have in the code, create new session
Now you can inspect your elements and try to click your button from inspector.
when i select values hidden its showing error as:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"partial link
text","selector":"vehicle-make"}
Here is my code:
package section5.advWays.locatingObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class CusXPathUsingAtt1 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver wd = new FirefoxDriver();
wd.manage().window().maximize();
Thread.sleep(5000); wd.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
Select YearSelectDropdown = new Select(wd.findElement(By.id("vehicle-year")));
YearSelectDropdown.selectByVisibleText("2011");
Select VehicleSelectDropdown = new Select(wd.findElement(By.id("vehicle-model")));
VehicleSelectDropdown.selectByVisibleText("228i xDrive Coupe");
}
}
How to select those dropdown using selenium webdriver?
There are two things:
I found that you first need to click on an element without which the Select menu won't open. So in my code, I'm first clicking on the element to enable select menu.
There are also some elements that aren't readily available. Say, for example the Year won't get enabled unless Make is entered.
Please see the code below:
WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Make')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
Select SelectMakedropdown = new Select(driver.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Year')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
Select YearSelectDropdown = new Select(driver.findElement(By.id("vehicle-year")));
YearSelectDropdown.selectByVisibleText("2011");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Model')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
Select VehicleSelectDropdown = new Select(driver.findElement(By.id("vehicle-model")));
VehicleSelectDropdown.selectByVisibleText("128i Cabriolet Base Model");
driver.quit();
UPDATE for Firefox:
I tried a lot, but I'm still unable to identify why isn't the selects working in Firefox. But I still managed to come up with a work around to do the needful. Here I'm using less use of clicks and more of features your application supports.
WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);
JavascriptExecutor executor = (JavascriptExecutor) driver;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
WebElement we1 = driver.findElement(By.xpath("//div[contains(text(),'Select Make')]"));
executor.executeScript("arguments[0].click();", we1);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
WebElement SelectMakedropdown = driver.findElement(By.id("vehicle-make"));
SelectMakedropdown.sendKeys("BMW");
SelectMakedropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
WebElement we2 = driver.findElement(By.xpath("//div[contains(text(),'Select Year')]"));
executor.executeScript("arguments[0].click();", we2);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
WebElement YearSelectDropdown = driver.findElement(By.id("vehicle-year"));
YearSelectDropdown.sendKeys("2011");
YearSelectDropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
WebElement we3 = driver.findElement(By.xpath("//div[contains(text(),'Select Model')]"));
executor.executeScript("arguments[0].click();", we3);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
WebElement VehicleSelectDropdown = driver.findElement(By.id("vehicle-model"));
VehicleSelectDropdown.sendKeys("128i Cabriolet Base Model");
VehicleSelectDropdown.sendKeys(Keys.ENTER);
It seems page require to click on dropdown first , Try this code :
wd.findElement(By.xpath("//*[#id='shopByVehicle-search-change']/div[1]/div[1]")).click();
Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
Above will run for sure.
When I am trying to open a site through TestNG, a security page is coming and for that I have click a link text "Continue to this website(not recommended)." that i have done through TestNG coding but it is giving java.lang.NullPointerException error.
and this is the code which i am trying......
public class Registration {
WebDriver driver;
WebElement element;
WebElement element2;
WebDriverWait waiter;
#Test(priority = 1)
public void register_With_Cash() throws RowsExceededException, BiffException, WriteException, IOException
{
File file = new File("D:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver=new InternetExplorerDriver();
driver.get("https://172.25.155.250/loginpage.aspx");
sleep(10000);
waiter.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Continue to this website (not recommended).")));
driver.findElement(By.linkText("Continue to this website (not recommended).")).click();
sleep(50000);
Thanx in advance for any kind of help.
you can use the below javascript to click the Continue to the Website(not recommended) link.
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");