Selenium webdriver- Not able to click on option which becomes visible on hover - selenium

Here i'm using linkedin for the learning purpose & want to perform the action on Edit profile=> work experience=> Add position. Here "Add position" button comes under work experience which becomes visible on hovering to that specific area. Look at my code,
driver.findElement(By.id("login-email")).clear();
driver.findElement(By.id("login email")).sendKeys("email id");
driver.findElement(By.id("login-password")).clear();
driver.findElement(By.id("login-password")).sendKeys("password");
driver.findElement(By.name("submit")).click();
driver.findElement(By.linkText("Home")).click();
driver.findElement(By.linkText("Profile")).click();
Thread.sleep(3000);
/* To scroll the page down */
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,250)", "");
/* To hover mouse on required option*/
Actions hover=new Actions(driver);
hover.moveToElement(driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")));
driver.findElement(By.xpath("id('background-experience')/div[1]/div/button[1]")).click();
Thread.sleep(5000);
/* To Edit company name */
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).clear();
driver.findElement(By.xpath("id('companyName-positionCompany-position-editPositionForm')")).sendKeys("testing company");
This is so on continued to edit work details but when i execute, it stops the page at work experience & not able to click on add position button to edit work.
It gives error on "edit company" code line. Can anyone help me to perform the same?

//Hover
Actions actions = new Actions(driver);
IWebElement menuHoverLink = driver.FindElement(By.XPath("//*[#id='background-experience']/div[1]/div/button[1]"));
actions.MoveToElement(menuHoverLink);
actions.Build().Perform();
driver.FindElement(By.XPath("//*[#id='background-experience']/div[1]/div/button[1]")).Click();

Related

Right click is not happening and element is getting clicked on the same tab

I have a Selenium script to execute the following steps:
Launch the website - https://vusevapor.com/
Hit "I am 21+" button
Move to element devices.
Right-click on ciro complete kit.
The issue is that the right-click menu remains as is, and the element is getting clicked on the same page.
Here is my code:
//website
driver.get("https://vusevapor.com/");
//clicking on i am 21+ button
driver.findElement(By.xpath("/html/body/aside/div[2]/div/div/div[2]/div/a[1]/span")).click();
Thread.sleep(5000);
//xpath of devices menu
WebElement devices = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/a/span"));
//move to element devices
Actions act = new Actions(driver);
act.moveToElement(devices).build().perform();
Thread.sleep(3000);
//xpath of ciro complete kit
WebElement ciroKit = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/ul/li[2]/ul/li[1]/a/span"));
//right click on ciro complete kit
//*****Issue********right click is happening but the element is getting clicked on the same tab and right click menu remains as is
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
try this:
option 1:
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
option 2-
Use Action and Robot class:
act.contextClick(ciroKit).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
Hope this helps.

Trying to hover on a menu and click on a link in the sub-menu but no luck

I am learning to hover over a menu and click on a link int he sub-menu.
The scenario is go to the URL "https://www.amazon.in", hover over "Hello Sign in" and then click on the link "Start here".
I can hover over "Hello Sign in" using moveToElement() and sub menu is opening, but couldn't click on the link "Start here".
Here is my code.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement startHere =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();
The link text includes a dot at the end, you missed it in your code, Try By.linkText("Start here.") or By.partialLinkText("Start here")
Please try the below modified code and it's working as expected. First you can find the new Customer Div and then directly access the start here link as below.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement newCustomer=driver.findElement(By.id("nav-flyout-ya-newCust"));
newCustomer.findElement(By.xpath(".//a")).click();
To access the URL https://www.amazon.in then hover over Hello Sign in and then click on the link Start here. you have to induce WebDriverWait for the element with text as Hello Sign in to be visible, then Mouse Hover over it and then induce WebDriverWait for the element with text as Start here. to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
new Actions(driver).moveToElement(signUp).build().perform();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
Browser Snapshot :

Selenium WebDriver Log Out from Facebook

Does anyone know how to click log out button? I tried using driver.findElement(By.linkText("Log out"));
But it returned an error saying element is not found. Is it because the list is dynamically generated?
You should try using WebDriverWait to wait until elementToBeClickable it works for me as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement accountSettings = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account Settings")));
accountSettings.click() //this will click on setting link to open menu
WebElement logOut = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
logOut.click() // this will click on logout link
Hope it helps...:)
I assume, after click on arrow button, Log Out button appers in ur code. So to click on that log Out button, use the below portion as cssSelector:
a[data-gt*='menu_logout']>span>span._54nh
driver.findElement(By.cssSelector("a[data-gt*='menu_logout']>span>span._54nh"));

How to click a button when it is covered by tooltips of other links above the button in selenium

I have a scenario where I need to click a button to edit the page, But above this edit button there are links.. when am running the code the mouse automatically moving over those links and masking the EDIT button.
Can Anyone Suggest me hoe to click that button
JavaScript will click on the element regardless its state (covered in your case)
//Java syntax, similar in other languages
WebElement button = driver.findElement(By.id("id")); //locate the button
JavascriptExecutor js = (JavascriptExecutor)driver; //initialize JavascriptExecutor
js.executeScript("arguments[0].click();", button); //click the button
You should hide your elements you don't want to appear on the page with something like
driver.execute_script(document.getElementById('THE_ID_OF_YOUR_ELEMENT').style.display='none';");
If you don't want to use the ID, you can also use the css selector, the xpath, etc...

How to get a tooltip text on mouseover using Selenium WebDriver

I am not able to get tooltip text after mouseover on icon tooltip. I need to get tooltip text,This is html code.
<a class="tooltip" onclick="SocialAuth.showOpenIDLoginWindow('google');_gaq.push(['_trackEvent','LoginForm','google-login','google login was clicked']);" href="javascript:void(0);"><span>We dont store your password or access your contacts on your Google account.</span><img class="social" height="39" width="40" src="/images/login/google.png"/>
The method to get text from a tool tip differs from a HTML one when its a Jquery ToolTip.
getAttribute() does not work when its a Jquery ToolTip. If you see the tooltip example at http://demoqa.com/tooltip/ , its a jquery tooltip.
Following code works here:
WebDriver driver=new FirefoxDriver();
driver.get("http://demoqa.com/tooltip/");
WebElement element = driver.findElement(By.xpath(".//*[#id='age']"));
Actions toolAct = new Actions(driver);
toolAct.moveToElement(element).build().perform();
WebElement toolTipElement = driver.findElement(By.cssSelector(".ui-tooltip"));
String toolTipText = toolTipElement.getText();
System.out.println(toolTipText);
A good reference is:
http://www.seleniumeasy.com/selenium-tutorials/how-to-verify-tooltip-text-with-selenium-webdriver-using-java
Use the below line of code for fetching the tool tip text from the Element.
String toolTipText = driver.findElement(By.id(element's id)).getAttribute("title");
You have to use Actions for this. in this am printing the mouse hover message in Google
Actions ToolTip1 = new Actions(driver);
WebElement googleLogo = driver.findElement(By.xpath("//div[#id='hplogo']"));
Thread.sleep(2000);
ToolTip1.clickAndHold(googleLogo).perform();
Perform mouse hover action using 'clickAndHold' method.
Get the value of Tool tip by using 'getAttribute' command
String ToolTipText = googleLogo.getAttribute("title");
Assert.assertEquals(ToolTipText, "Google");
Thread.sleep(2000);
System.out.println("Tooltip value is: " + ToolTipText);