Selenium : Automating LinkedIn - Profile Icon - java

I am new to Selenium and trying to use Actions class to mouseover on the Profile icon available on linked in site to open the menu that appears on Mouseover of profile image.
Below is my code and when it reaches on to those lines the error comes : Unable to locate element..
This is happening with all the icons available on Linked on top bar ( messages / Flag icon etc.
Code :
public class LinkedIn {
WebDriver driver = new FirefoxDriver();
#BeforeTest
public void setUp() throws Exception {
String baseUrl = "http://www.linkedin.com/";
driver.get(baseUrl);
}
#Test
public void login() throws InterruptedException
{
WebElement login = driver.findElement(By.id("login-email"));
login.sendKeys("*****#gmail.com");
WebElement pwd = driver.findElement(By.id("login-password"));
pwd.sendKeys("*****");
WebElement in = driver.findElement(By.name("submit"));
in.click();
Thread.sleep(10000);
}
#Test
public void profile() {
// here it gives error to me : Unable to locate element
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='img-defer-id-1-25469']"));
action.moveToElement(profile).build().perform();
driver.quit();
}
}

It seems you have used incorrect xpath , Kindly check below example to mouse hover on Message button :
Thread.sleep(5000);
Actions action = new Actions(driver);
WebElement profile = driver.findElement(By.xpath("//*[#id='account-nav']/ul/li[1]"));
action.moveToElement(profile).build().perform();
Correct Xpaths are :
For Message Icon : "//*[#id='account-nav']/ul/li[1]"
For Connection Icon : //*[#id='dropdowntest']
Above code I just tested and working fine so will work for you.

Related

org.openqa.selenium.StaleElementReferenceException: element is not attached to the page document

I am trying to test the GeeksForGeeks UI. When I click the tutorials dropdown, then select languages and select Java, it links to a new page and the following error occurs org.openqa.selenium.StaleElementReferenceException. How can I solve this issue? I have tried all the possible solutions from stackoverflow.
public class SeleniumTest {
public static WebDriver driver;
#BeforeClass
public static void setupClass() {
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
}
#Before
public void setup() {
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
#After
public void after() {
driver.close();
}
#Test
public void testGeeksForGeeksR() throws InterruptedException {
driver.get("https://www.geeksforgeeks.org/");
WebElement tutorialsMenu = driver.findElement(By.className("header-main__list-item"));
tutorialsMenu.click();
List<WebElement> tutorialsList = tutorialsMenu.findElements(By.tagName("li"));
for (WebElement li : tutorialsList) {
if (li.getText().equals("Languages")) {
li.click();
List<WebElement> languages = driver.findElements(By.tagName("a"));
for (WebElement a : languages) {
if (a.getText().equals("Java")) {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(a));
a.click();
WebElement title = driver.findElement(By.className("entry-title"));
assertEquals("Java Programming Language", title.getText());
}
}
}
}
Thread.sleep(6000);
}
}
Solution:
#Test
public void testGeeksForGeeksR() throws InterruptedException {
driver.get("https://www.geeksforgeeks.org/");
WebElement tutorialsMenu = driver.findElement(By.className("header-main__list-item"));
tutorialsMenu.click();
List<WebElement> tutorialsList = tutorialsMenu.findElements(By.tagName("li"));
WebElement javaLanguage = null;
for (WebElement li : tutorialsList) {
if (li.getText().equals("Languages")) {
li.click();
List<WebElement> languages = driver.findElements(By.tagName("a"));
for (WebElement a : languages) {
if (a.getText().equals("Java")) {
javaLanguage = a;
break;
}
}
}
}
javaLanguage.click();
driver.switchTo().activeElement();
WebElement title = driver.findElement(By.className("entry-title"));
assertEquals("Java Programming Language", title.getText());
Thread.sleep(3000);
}
After clicking on the a element with Java text the Java Programming Language page is opened.
At this point all the element references collected on the previous page are becoming Stale.
Generally, each Selenium WebElement object is actually a reference (pointer) to physical web element object.
So, when you are opening another web page or refreshing the existing page (reloading the web elements there) all the references to the web elements on the previous web page are no more valid.
In the Selenium terminology this situation is called Stale Element.
Getting back to your specific code flow.
Looks like your target here is to open the Java Programming Language page. If so, all what you are missing here is to exit your loop once that page is opened and finish the test.
In case you wish to continue opening another tutorials from the menu on the main page you will have to go back from the internal page you opened and then get all the elements you wish to use there again.

Unable To Find Locator After Giving Permission "com.android.packageinstaller:id/permission_allow_button" In Selenium Appium

I I am automating application with selenium Appium
Selenium later version
Appium 1.9.1
Whenever application launch always permission pop up showing successfully I can click on permission using automation but after giving permission appium is not able to find elements in the application .
If I click manually in the application, for example, I click on the next button and again click on the back button then appium code is working fine.
Can anyone help me how i can run my script without doing manual interaction
Page class
public class DriverSignUpPages {
AndroidDriver<MobileElement> driver;
public static final String packageName = "co.wapanda.android.beta";
public DriverSignUpPages(WebDriver driver){
this.driver = (AndroidDriver<MobileElement>)driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
//-Storing element for pop up related access or permission
#AndroidFindBy(id = "com.android.packageinstaller:id/permission_allow_button")
MobileElement permAlert;
public void allowAccessLocation() {
if(permAlert.isDisplayed()) {
permAlert.click();
} else {
}
}
//-Store signUp locator and performed click action
#AndroidFindBy(id = packageName+":id/btn_signup")
MobileElement signUpButton;
public void clickOnSignUpButton() {
signUpButton.click();
}
//-Store driversignup locator value and performed click action
#AndroidFindBy(id = packageName+":id/btn_driver")
MobileElement driverSignUp;
public void clickOnDriverSignUp() {
driverSignUp.click();
}
------------------------------
Test case class
public class ValidateDriverProcess {
AppLaunch applaunch;
DriverSignUpPages dprocess;
AppiumDriver<MobileElement> driver;
#BeforeMethod
public void launchApplication( ) {
applaunch = new AppLaunch();
driver = (AppiumDriver<MobileElement>) applaunch.launchApplication();
}
#Test
public void validateDriverSignupProcess() {
try {
DriverSignUpPages dprocess = new DriverSignUpPages(driver);
Thread.sleep(5000);
dprocess.allowAccessLocation();
Thread.sleep(5000);
dprocess.clickOnSignUpButton();
Thread.sleep(5000);
dprocess.clickOnDriverSignUp();
Thread.sleep(5000);
dprocess.enterSignUpDetail("test#abc.com", "9898989891", "123456");
Thread.sleep(5000);
dprocess.enterFirstAndLastNameInSignUp("Raj", "");
Thread.sleep(5000);
dprocess.enterVerificationCode("757575");
} catch (Exception e) {
e.printStackTrace();
}
}
#AfterMethod
public void closeApplication() {
driver.quit();
}
}
------------
In appium log error showing
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result
{"status":7,"value":"No element found"}
not able to find this line
//-Store signUp locator and performed click action
#AndroidFindBy(id = packageName+":id/btn_signup")
MobileElement signUpButton;
There is an open issue with appium: After dismissing the alert, appium is no longer detecting any of the fields.
Workarounds :
You can put the app in the background and launch again. driver.runAppInBackground(5);
OR
Get coordinates (x, y) of any element on screen before opening dialog interact with dialog (e.g. select elements, close etc.) tap by coordinates (x, y) after closing dialog: new TouchAction(driver).tap(x, y).perform();

Unable to perform mouse hover action which clicking on a link

I am using www.flipkart.com,where I want to hover over "Appliances" and click on "Television".
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.flipkart.com");
driver.manage().window().maximize();
Thread.sleep(1000);
WebElement mainMenu = driver.findElement(By.xpath("//a[#title='Appliances']"));
WebElement submenuxpath = driver.findElement(By.xpath("//li[#class='Wbt_B2'][2]//li[#class='_1KCOnI _1h5QLb']//a[#title='Televisions']"));
Actions builder = new Actions(driver);
builder.moveToElement(mainMenu).perform();
//builder.moveToElement(submenuxpath).click().perform();
//driver.click(submenuxpath);
Thread.sleep(1000);
driver.close();
}
It is able to hover over on "Appliances". But I am getting error in driver.click(submenupath) or builder.moveToElement(submenuxpath).click().perform(). Where I am going wrong?
driver.click(submenupath) error: The method click(WebElement) is undefined for the type WebDriver. Quick fix : add cast to driver. Even if I am doing add cast,it is not working then. For builder.moveToElement(submenuxpath).click().perform(),no error is coming,but it is not clicking also.

Locate button id with selenium

I want to locate button ID using selenium web driver. I tried this code:
#Test
public void hello()
{
RemoteWebDriver driver = BrowserFactory.getDriver("chrome", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver.get("http://jenkins.mws.com:8080");
WebDriverWait waitPage = new WebDriverWait(driver, 20);
WebElement until = waitPage.until(ExpectedConditions.presenceOfElementLocated(By.id("ready")));
if (until.isDisplayed()){
System.out.println("button is displayed");
}
}
But I get Timed out after 20 seconds waiting for presence of element located by: By.tagName: button
Here is the code that I want to test:
http://pastebin.com/up29pSRQ
Hwo I can locate tag button?
<button> is inside an iFrame. Switch to the iFrame first driver.switchTo().frame("iwg-game-full"); and then try.

Drag and drop the image into the BOX- selenium with java

I want to perform drag and drop in W3school web page using selenium. Code is working fine but output is not showing on the webpage.
link is :- http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
My code is :-
public String dragAndDrop(String object,String data){
APP_LOGS.debug("waiting for popup closer");
try{
driver.switchTo().frame("iframeResult");
WebElement element = driver.findElement(By.xpath(".//*[#id='drag1']"));
WebElement target = driver.findElement(By.xpath(".//*[#id='div1']"));
(new Actions(driver)).dragAndDrop(element, target).build().perform();
}catch(Exception e){
return Constants.KEYWORD_FAIL+" -- Unable to drag"+e.getMessage();
}
return Constants.KEYWORD_PASS;
}
We can also interact with keyboard / mouse events using Actions class and robot class in Selenium. I have used Robot class to resolve your issue.
The Robot class is present in java.awt package. You can check all the methods in the docs.
public static void Task1() throws AWTError, AWTException, InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop");
driver.switchTo().frame("iframeResult");
WebElement element1 = driver.findElement(By.xpath(".//img[#id='drag1']"));
WebElement element2 = driver.findElement(By.xpath(".//*[#id='div1']"));
Actions action = new Actions(driver);
Point element3 = driver.findElement(By.xpath(".//*[#id='drag1']")).getLocation();
int i=element3.getX()+800;
int b=element3.getY()+250;
Robot robot = new Robot();
robot.mouseMove(i, b);
// Press left click of mouse
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.delay(4000);
robot.mouseMove(i+20, b-120);
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Thread.sleep(10000);
driver.close();
}

Categories

Resources