After switching to new tab not able to locate the element - selenium-webdriver

In wordpress site,I am opening a link in a new tab to add a new post, I am able to enter the post title but not able to click on publish button. I have tried all possible locators id, xpath...etc.
can someone help me understand this issue. This screenshot shows page after i open link in new tab.
Here is my sample code:
driver.get("http://localhost/wordpress/wp-login.php");
driver.manage().window().maximize();
driver.findElement(By.id("user_login")).sendKeys("admin");
driver.findElement(By.id("user_pass")).sendKeys("q1w2e3r4");
driver.findElement(By.id("wp-submit")).click();
Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='welcome-panel']/div/div/div[2]/ul/li[1]/a")));
actions.keyDown(Keys.CONTROL).perform();
actions.keyDown(Keys.SHIFT).perform();
driver.findElement(By.xpath(".//*[#id='welcome-panel']/div/div/div[2]/ul/li[1]/a")).click();
actions.keyUp(Keys.SHIFT);
actions.keyUp(Keys.CONTROL);
driver.findElement(By.id("title")).sendKeys("TestPost"); //Working
driver.findElement(By.id("publish")).click(); //not working
Thanks in advance

Related

How to write selenium code for clicking on the link and opening the link in new tab

Hi Please let me know how to
Write selenium code for right click on the link and open the link .I am aware that i can use keyboard functions but i am bit confused
try the following code ... this is the code for right clicking on "About" link which appears in google page.. u can open that in new tab by selecting options in context menu.....
WebDriver driver = new ChromeDriver();
driver.get("http:\\google.com");
WebElement ele= driver.findElement(By.linkText("About"));
Actions ac=new Actions(driver);
Thread.sleep(2000);
ac.contextClick().sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_UP).sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
driver.quit();

Selenium webdriver (Double click on a search result displayed as label)

I am very new to selenium and need to double click on a label(appears after a successful search result).
Please help.
enter image description here
Attached is the page look like.
You should use the Actions() class as this includes a 'double-click' action.
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//select[#id='groupselect']/#option[#value='942711']"))).doubleClick().build().perform();
OR
Actions action = new Actions(driver);
WebElement element=By.xpath("//select[#id='groupselect']/#option[#value='942711']"));
//Double click
action.doubleClick(element).perform();
//Mouse over
action.moveToElement(element).perform();
//Right Click
action.contextClick(element).perform();
Hope it will help you :)

Selenium webdriver automation, automating context menu options

I am not able to click on any element after doing right click in selenium(Java).
Its just doing a right click and is not clicking of any of the options like open in new tab...Instead its just doing a normal click after doing a right click.Can anyone please help me.Below is my code
System.setProperty("webdriver.chrome.driver","C:\Selenium\chromedriver.exe");
WebDriver wd=new ChromeDriver();
wd.get("http://google.com");
Thread.sleep(3000);
//Point a=wd.findElement(By.linkText("Testing")).getLocation();
WebElement b=wd.findElement(By.linkText("About"));
Actions action=new Actions(wd);
//action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
action.moveToElement(b);
Thread.sleep(4000);
//action.contextClick(b);
action.contextClick(b);
action.sendKeys(Keys.ARROW_DOWN).sendKe ys(Keys.ENTER).build().perform();
I have tried via context click and move to element as well but no result.Thanks in advance..
Might be this is what you want:
To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-
Actions action= new Actions(driver);
action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Soure: Select an Option from the Right-Click Menu in Selenium Webdriver - Java
As per request in the comments adding alternative way to open a link in new tab.
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("http://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 30);
Thread.sleep(3000);
WebElement b=driver.findElement(By.linkText("About"));
Actions action=new Actions(driver);
action.moveToElement(b).perform();
Thread.sleep(4000);
action.keyDown(Keys.CONTROL);
action.click();
action.keyDown(Keys.CONTROL).build().perform();
//action.sendKeys(Keys.RETURN).perform();
Thread.sleep(4000);
driver.quit();
The build() method is used compile all the listed actions into a single step. We use build() when we are performing sequence of operations. We can directly use perform() if we are performing single action. You can read more about Actions class.
Also, re-iterating that I was unable to find the root of the issue in the limited time I had, so I placed a work around. So like we use the shortcut CTRL+CLICK to open the link in new tab manually, You might need to find out the shortcut for what you need, you can refer this : https://support.google.com/chrome/answer/157179?hl=en
Hope it helps :)

Confluence Create Button

I'm creating a new Page in Confluence with Selenium.
However after clicking create, I'm unable to click the send create button.
driver.findElement(By.id("create-page-button")).click();
driver.findElement(By.xpath("//div[#id='create-dialog']/div/div[2]/button")).click();
driver.findElement(By.id("content_title")).sendKeys("Test Case 1");
driver.findElement(By.id("rte-button-publish")).click();
The code errors on:
Unable to locate element: {"method":"id","selector":"content_title"}
as the new page has not been created.
This is what it took to create a page for myself:
driver.findElement(By.id("create-page-button")).click();
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.findElement(By.xpath("//*[#id='create-dialog']/div/div[2]/button")).getText().equals("Create");
}
});
driver.findElement(By.xpath("//*[#id='create-dialog']/div/div[2]/button")).click();
driver.findElement(By.xpath("//*[#id='content-title']")).sendKeys("My Test Page");
driver.findElement(By.id("rte-button-publish")).click();
I tried using the By.id for the content-title input box but it didn't work with the error being the same one as you received however doing it by xpath worked.
Also the NewDriverWait was added since the dialog box is loaded in and if you click the Create button too fast it would actually be the Next button which would result in the page not being created.

File Upload button is not working using Selenium webdriver

Am trying to upload a jpeg image to my application . But on webpage we can upload other jpeg images as well in their respective section. Here as per the HTML, Upload button have the same attributes defined for other upload sections.
However ,When I inspect the xpath for Unique element for Upload button , I found the below xpath=//table/tbody/tr[1]/td[1]/div/div/div/input[#class='bttnUpload'] and verified using xpath checker
Below is Webdriver code used
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//Test URL
driver.get("URL");
// UN & PWD
driver.findElement(By.xpath("//input[#id='UserName']")).clear();
driver.findElement(By.xpath("//input[#id='UserName']")).sendKeys("UN");
driver.findElement(By.xpath("//input[#id='Password']")).clear();
driver.findElement(By.xpath("//input[#id='Password']")).sendKeys("pwd");
driver.findElement(By.xpath("//input[#id='btnSubmit']")).click();
//Validate the User logged in
if(driver.getPageSource().contains("UN Kumar"))
System.out.println("UN is logged in");
else
System.err.println("UN not logged in");
//Click on Asset Menu and LookUp Assets sub menu
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("//a[#href='http://ccqweb1.cloudapp.net//Main.aspx? MenuId=15']"))).build().perform();
driver.findElement(By.linkText("Lookup Assets")).click();
//Search for Loan Number
driver.findElement(By.xpath("//input[#id='ctl10_txtLoannumber']")).sendKeys("787878717");
driver.findElement(By.xpath("//input[#id='ctl10_cmdSearch']")).click();
Thread.sleep(2000);
// Get the window handle before clicking on link
String winHandleBefore = driver.getWindowHandle();
//Click on Initial Occupancy task
driver.findElement(By.xpath("//a[#id='ctl09_gvPendingTasks_ctl02_lnkTask']")).click();
Thread.sleep(2000);
// Switch to New Window
String child = driver.getWindowHandle();
for(String parent : driver.getWindowHandles()){
driver.switchTo().window(parent);
}
/**Exterior Photos tab **/
//Click on Exterior tab
driver.findElement(By.xpath("//a[#id='ctl09_btnExteriorPhotos']")).click();
//Front View* `enter code here`
//Click on Manual Upload button
driver.findElement(By.xpath("//div/input[#onclick='DisplayManualUpload(event)']")).click();
WebElement FileUpload_FrontView= driver.findElement(By.xpath("//input[#class='fleManuleUpload']"));
FileUpload_FrontView.sendKeys("\\Front_view.jpeg");
Thread.sleep(500);
driver.findElement(By.xpath("//table/tbody/tr[1]/td[1]/div/div/div/input[#class='bttnUpload']")).click();
The Upload image is getting selected but Upload button not is clicked so am not able to upload the file.
Can you help me to solve this ?
I'm pretty sure it's just a typo.
As well as for the upload locator
(//input[#class='fleManuleUpload'] ->e.g. fleManualUpload )
as for the upload button
([#class='bttnUpload'] -> btnUpload ).
In General: Why don't you use more consistent css locators or just ids?
By.xpath("//input[#id='UserName']"
use easily:
By.id("UserName")
To give you a better answer please upload the error message when you run this.
The reason behind this is:
FileUpload_FrontView.sendKeys("C:\\Users\\prabhakar.y\\Desktop\\Pics for Uploading\\Front_view.jpeg");
You are having space in foldername - "Pics for Uploading".
Try out by giving
File f = new File("Pics for Uploading\\Front_view.jpeg");
FileUpload_FrontView.sendKeys(f.getAbsolutePath());
and this folder should be inside your current workspace (Its a good practice). Or else remove space from foldername.

Resources