Upload file using selenium web driver - java

I have already check and search for same question and there are lot of solution but no one is working for me so posing question here.
I am doing practice of selenium web driver. I am using this form for practice : http://www.toolsqa.com/automation-practice-form/
Now , I have 3 issues in that.
1 - There are first 2 links called "Partial link test" & "List test" which I am able to click on, using "findelement", but I want to open both link in NEW TAB in same browser.
2 - I am not able to upload file. My code is not working for that element.
3 - How can I select particular value from dropdown of "Continents"??
My code is given below :
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com/automation-practice-form/");
driver.manage().window().maximize();
**driver.findElement(By.linkText("Partial Link Test")).click();
driver.findElement(By.linkText("Link Test")).click();**
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.name("firstname")).sendKeys("Tester");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.name("lastname")).sendKeys("Tester");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("sex-0")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("exp-2")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("datepicker")).sendKeys("01/01/1985");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("profession-1")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
**driver.findElement(By.id("photo")).sendKeys("C:/Users/Public/Pictures/Sample Pictures/Desert.jpeg");**
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(600);
driver.findElement(By.id("tool-0")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
**driver.findElement(By.id("continents")).click();**
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Please help to correct my code.

I have added the answers inline to each of your questions, below. Also, an advice, is to use Implicit wait only once at the top while creating a browser instance, as its scope is the whole class itself. So, once declared, then selenium will wait a maximum of that time, for detecting an element. It can be rather overridden by using Explicit waits for certain elements, if necessary Please see this link for better understanding Implicit and Explicit waits:
1 - There are first 2 links called "Partial link test" & "List test" which I am able to click on, using "findelement", but I want to open both link in NEW TAB in same browser.
//Clicking and opening Partial Link Text in new tab
WebElement element = driver.findElement(By.linkText("Partial Link Test"));
Actions act = new Actions(driver);
act.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
//Clicking and opening Link Text in new tab
element = driver.findElement(By.linkText("Link Test"));
act.contextClick(element).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
2 - I am not able to upload file. My code is not working for that element.
The path of the file must be like this:
driver.findElement(By.id("photo")).sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desert.jpg");
3 - How can I select particular value from dropdown of "Continents"??
You can use Select class for that like below. It will select the option "Australia".
Select sel = new Select(driver.findElement(By.id("continents")));
sel.selectByVisibleText("Australia");

Open link in new Tab:
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);

Related

Cant select option from dropdown which is inside iframe - selenium - java8

I am trying to select option from the drop down in iframe but it is not getting selected.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://verify-taxcerts.floridarevenue.com");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.id("ivuFrm_page0ivu3")));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("OFFM.SellerVerView.TaxTypeDDKey")).click();
driver.findElement(By.id("OFFM.SellerVerView.TaxTypeDDKey")).sendKeys("Sales and Use Tax");
Error
org.openqa.selenium.NoSuchElementException: Unable to locate element: #OFFM\.SellerVerView\.TaxTypeDDKey
Use case : i am trying to select option "sales and Use Tax"
what i am doing wrong ?
selenium version :3.14
ff : 70
Thanks
There's one more frame with isolatedWorkArea id:
driver.switchTo().frame(driver.findElement(By.id("ivuFrm_page0ivu3")));
// one more iframe
driver.switchTo().frame(driver.findElement(By.id("isolatedWorkArea")));
driver.findElement(By.id("OFFM.SellerVerView.TaxTypeDDKey")).click();

Not able to click on a button in selenium webdriver

webDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[1]/section[1]/div/div/a/button")).submit();
'View positions' button is not clicking with the above code.What is happening in the web page?
You see the HTML for this page is
So, you can use the CSS selector for this as
WebDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.cssSelector("button.button.teal").click();
And then proceed with doing whatever is necessary. I executed with this in my Python code and it works fine.
Also, you will need to provide the Gecko executable path while calling for the FirefoxDriver()
The way I have done it before is to use the click handler.
driver.findElement(By.cssSelector(".profile-actions .primary_button > span")).click();
I'm sure you could also select the element by xpath rather than CSS in the above line. It's a similar question to this one.

Not able to locate element in selenium 3.0

I am writing a script to locate "login" and click on it for an web based application but I am getting exception:
no such element: Unable to locate element
My code:
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wayn.com"); //to find login
driver.findElement(By.xpath("//*[#id='TopMenu']/div[1]/div/div[2]/login-buttons/div/div[1]/div[1]"))
.sendKeys(Keys.ENTER);
Try this way.
driver.get("http://www2.wayn.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='button big left red ng-isolate-scope'][#wayn-log-click='loginButtonsLogClick']")).click();
Explanation of xpath: Use class and wayn-log-click attribute along with <div> tag.
Suggestion :- Instead of using absolute xpath, use relative xpath.
try waiting a little for element to be visible:
WebDriverWait wait =new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");

Selenium Webdriver clickAndHold() error on Firefox

I try to select multiple options from list but it does not select particular option it select from first choose options to last choose option and give some error like:
Cannot perform native interaction: Could not get node for element - cannot interact
My code is looks like
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/selectable/");
driver.manage().window().maximize();
driver.switchTo().frame(driver.findElements(By.tagName("iframe")).get(0));
WebElement multiSelectDropDown=driver.findElement(By.className("ui-selectable"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("li"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).
clickAndHold(dropdownlists.get(4)).click()
.build().perform();
Can any one tell me why this is not working is there any problem in my code.
I think you need to change this
builder.clickAndHold(dropdownlists.get(0)).
clickAndHold(dropdownlists.get(4)).click()
.build().perform();
This should be looks like
builder.clickAndHold(dropdownlists.get(0)).moveToElement(dropdownlists.get(4)).
release().build().perform();
because in real world click the mouse and drag to the other element so moveto another element and then release the mouse.

Selenium - WebDriver not recognizing elements but IDE recognizes same elements

I am working on a sample assignment on Thomson Holidays website (http://www.thomson.co.uk/holidays.html). On left hand side there is a Holiday Search panel. I am unable to recognize any of these elements in WebDriver. However, in IDE these elements are recognized. Need more info on this as it is the first time i am experiencing such an issue. Below is the code sample:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();
Thread.sleep(5000);
Because they're in the iframe, you need switch to the iframe first.
Two lines added to your existing code as follows:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.thomson.co.uk/holidays.html");
// optional, unnecessary in your case
// driver.switchTo().defaultContent(); // make sure outside of all iframes
// switch to search frame
WebElement searchFrame = driver.findElement(By.cssSelector("iframe[src='/thomson/page/byo/search/usp.page']"));
driver.switchTo().frame(searchFrame);
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();
The search panel is inside an iframe. As the iframe is the first iframe, you can use the below code.
driver.get("http://www.thomson.co.uk/holidays.html");
// switch to search frame
driver.switchTo().frame(0);
driver.findElement(By.id("searchbutton")).click();
driver.findElement(By.id("holidayAttribute_1")).click();
driver.findElement(By.id("holidayAttribute_2")).click();
driver.findElement(By.id("holidayAttribute_3")).click();

Categories

Resources