How to click on a button with href attribute on webpage - selenium

I have used the below code to click on login button but it is not working:
Trial 1:
driver.findElement(By.xpath("//a[text()='https://www.luxproflashlights.com/customer/account/login/referer/aHR0cHM6Ly93d3cubHV4cHJvZmxhc2hsaWdodHMuY29tLw%2C%2C/']")).click();
Trial 2:
driver.findElement(By.linkText("Log In")).click();

If the element contains both the href attribute and the text Log In you can use either of the following Locator Strategies:
partialLinkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Log In"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(#href, 'luxproflashlights') and contains(., 'Log In')]"))).click();

Related

How to click the link icon in CK editor

When I click the link icon in ck editor initially it worked, but when I re-run the code, it's not clicking the link icon in the ck editor.
This is the command I used initially:
driver.findElement(By.xpath("//*[#id='cke_29']/span[1]")).click();
Use WebDriverWait to handle dynamic element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[starts-with(#id,'cke_')][#class='cke_button cke_button__link cke_button_off']"))).click();
As #akshay-patil commented use the a tag. The reason is that the <a> tag is the link itself... not the span!
You should use:
driver.findElement(By.xpath("//*[#id='cke_29']")).click();
Hope this helps!
The desired element is a JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cke_button.cke_button__link.cke_button_off[id^='cke_'][title^='Link']>span.cke_button_icon.cke_button__link_icon"))).click();
Using XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='cke_button cke_button__link cke_button_off' and starts-with(#id,'cke_')][starts-with(#title,'Link')]/span[#class='cke_button_icon cke_button__link_icon']"))).click();

Unable to Locate an element for Salesforce Lightning

Technology:
Salesforce
Lightning
Selenium webdriver
Html:
Code Trials:
driver.findElement(By.xpath("//a[#title='Acq Prospects']"));
driver.findElement(By.linkText("Acq Prospect"));
Error:
Unable to find an element
You simply need to wait object to be visible. You can use the following code;
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.Until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#title='Acq Prospects']")));
For more informations; link.
Note: Also ensure that there is no any element that overlay your object which you are trying to find.
To locate the desired element you need to induce WebDriverWait for the elementToBeClickable and you can use either of the following solutions:
cssSelector:
WebElement Acq_Prospects = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.slds-context-bar__label-action.dndItem[href='/lightning/o/Acq_Prospect__c/home']")));
xpath:
WebElement Acq_Prospects = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='slds-context-bar__label-action dndItem' and #href='/lightning/o/Acq_Prospect__c/home']")))

How to locate the element as per the HTML provided?

<a class="action showcart active" data-bind="scope: 'minicart_content'" href="http://52.14.171.179/xstpl/checkout/cart/">
I have tried this one but unable to proceed -
driver.findElement(By.linkText("cart")).click();
As per the HTML you have provided to invoke click() the element you have to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.action.showcart.active[href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='action showcart active' and #href='http://52.14.171.179/xstpl/checkout/cart/']"))).click();
findElement(By.partialLinkText("cart"))
And make sure that the string "cart" is case sensitive

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"));