Selenium xpath not clicking on Image link - java

Im trying to use selenium to select the first result in a search.
To click on the first image im using xpath way to find the first result from the search.
The code is
driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a")).click();
and from using the f12 and then ctrl f tools on the website it shows that I have the correct xpath
However, it is not clicking on the Image.
Here is the website that I am trying to test if it's any help.
https://www.dunnesstores.com/search?keywords=lamp

To click() on the first result from the search you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button#onetrust-accept-btn-handler"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ol.product-list.product-list-main > li a"))).click();
xpath:
driver.get("https://www.dunnesstores.com/search?keywords=lamp")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#id='onetrust-accept-btn-handler']"))).click();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//ol[#class='product-list product-list-main']/li//a"))).click();
Browser Snapshot:

driver = webdriver.Chrome()
driver.get('https://www.dunnesstores.com/search?keywords=lamp')
accept = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,
"#onetrust-accept-btn-handler")))
accept.click()
driver.find_element_by_css_selector(
'[class="c-product-card__link"]').click()
You have to first click accept cookies , then find element using class or xpath or css

Try:
element = driver.findElement(By.xpath("//*[#id='category-search-wrapper']/div[2]/div/ol/li[1]/article/a"))
driver.executeScript("arguments[0].click();", element)
With this code you avoid the elements that obscures it and the elements that are not disponible in your actual screen (out of scroll).

Related

How to click on a radio button using Selenium

I'm trying to click on a radio button in selenium java but it's not clicking.
This is the inspect on the radio button:
<input name="timeSyncType" class="radio" id="radioNTP" type="radio" ng-click="oSettingTimeInfo.szTimeMode='NTP'" ng-checked="oSettingTimeInfo.szTimeMode=='NTP'">
I tried this
WebElement radio = driver.FindElement(By.id("radioNTP"));
radio.click();
also I tried this
WebElement rbutton = driver.findElement(By.xpath("//input[#id='radioNTP']"));
rbutton.click();
but no luck, any help?
Firs you will have to make sure that this id radioNTP is unique in HTMLDOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the //input[#id='radioNTP'] and see, if your desired element is getting highlighted with 1/1 matching node.
If yes, then there are quite a few ways to perform click in Selenium.
Code trial 1:
Thread.sleep(5);
driver.findElement(By.xpath("//input[#id='radioNTP']")).click();
Code trial 2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP']"))).click();
Code trial 3:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
Code trial 4:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("//input[#id='radioNTP']"));
new Actions(driver).moveToElement(button).click().build().perform();
If No, then you will have to change the locator and use the one which represent the desire node.
PS: Thread.sleep(5); is just to solve this problem, if any of the above code works, Please replace the web element with WebDriverWait.
To click on the element you can use either of the following locator strategies:
cssSelector:
driver.findElement(By.cssSelector("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']")).click();
xpath:
driver.findElement(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]")).click();
However, as the element is an Angular element so ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#radioNTP[name='timeSyncType'][ng-click^='oSettingTimeInfo']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='radioNTP' and #name='timeSyncType'][starts-with(#ng-click, 'oSettingTimeInfo')]"))).click();

Automation: How to click on div role="radio" with only aria-label identifier?

I have the following code written in Java with Selenium Webdriver, but it's not clicking the div with the star rating
driver.get("https://goo.gl/maps/gLCX3PitJT1cXr9v9");
driver.findElement(By.xpath("//button[#data-value=\"Escribir una opinión\"]")).click();
driver.switchTo().frame(2);
driver.findElement(By.xpath("//textarea")).sendKeys("This is just a test");
driver.findElement(By.xpath("//span[#aria-label=\"Cuatro estrellas\"]")).click();
Between each line, I also added a Thread.sleep(5000) just to ensure the page fully loads.
The only clear identifier that I see is the aria-label.
Manual steps:
Open URL: https://goo.gl/maps/gLCX3PitJT1cXr9v9
Click "Escribir una opinión" or "Write a review" button
Type in the review field: "This is just a test"
Select the star rating: 4 stars
Click "Publicar" or "Post" (or Publish)
The Xpath for the "four-star" element is wrong. It should be //div[#aria-label='Cuatro estrellas']. You had span instead.
Can you try below
driver.findElement(By.xpath("//div[#class="s2xyy"][4]")).click();
OR
driver.findElement(By.xpath("//div[#role="radio"][3]")).click();
it is working for me
To click() on star rating 4 stars as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.goog-reviews-write-widget")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[aria-label='Four stars']"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#class='goog-reviews-write-widget']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#aria-label='Four stars']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?

Selenium: How to get text out of a div

I want to get a text which is inside a div. I tried to find help from other questions, but most of them say .getText() which doesnt exist anymore.
This is the inspect:
Im using a chrome extension with which i can copy out the XPath, but it doesnt reads the text inside the div. It copies me this here:
//div[#class='mud-alert-message']
To print the text Die Datei... you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.mud-alert-message")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='mud-alert-message']")).getText());
Ideally, to extract the text you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.mud-alert-message"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='mud-alert-message']"))).getAttribute("innerHTML"));
I believe is something like this:
WebElement element = new WebDriverWait(driver, ...).until(ExpectedConditions.elementToBeXXX(By.LOCATOR_TYPE));
String text = element.getText();

Not able to find an element in iframe using Xpath and Selenium

I am trying to inspect an element in Iframe
I am able to succesfully get into iframe but when trying to inspect element -Exception comes element not intertracble
Below is the html part and the search button I am trying to click
I tried xpath and inspection like following
//driver.findElement(By.className("gsc-search-button-v2")).click();
//driver.findElement(By.cssSelector("button.gsc-search-button")).click();
//driver.findElement(By.xpath("//button[text()='gsc-search-button']")).click();
//driver.findElement(By.cssSelector("button[class='gsc-search-button gsc-search-button-v2']")).click();
//driver.findElement(By.cssSelector(".gsc-search-button.gsc-search-button-v2")).click();
//driver.findElement(By.xpath("//button[contains(#class, 'gsc-search-button gsc-search-button-v2")).click();
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
Also by giving waits also like
WebDriverWait wait = new WebDriverWait(driver, 30);
//wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".gsc-search-button.gsc-search-button-v2"))).sendKeys(Keys.ENTER);
html code
To click() on the element to search, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe_cssSelector")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td.gsc-search-button > button.gsc-search-button.gsc-search-button-v2"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[#class='gsc-search-button']/button[#class='gsc-search-button gsc-search-button-v2']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
How to click within the username field within ProtonMail signup page using Selenium and Java

Unable to click or sendKeys in ajax overlay popup coming and its showing Element not Intractable exception using selenium and java

I have been practicing to automate few scenarios for web application way2Automation.com and struggling to enter the text in Registration form coming as the popup.
I have done some research already and tried many ways mentioned below:
a) Using WebDriverWait and explicit wait
b) Using Implicit wait and Thread.sleep
c) Using JavaScriptExecutor
But none of them worked for me and I am still stuck to register the user. Would really appreciate the help. Below are the artificats
URL: http://way2automation.com/way2auto_jquery/index.php
Code trials:
1)
// WebElement ele = driver.findElement(By.xpath("//*[#id='load_form']/div/div[2]/input"));
// JavascriptExecutor executor = (JavascriptExecutor)driver;
// executor.executeScript("arguments[0].click();", ele);
// WebElement button = driver.findElement(By.xpath("//*[#id=\"load_form\"]/div/div[2]/input"));
// new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(button));
You need to handle windows
Try the below code and let me know updates
driver.get("http://way2automation.com/way2auto_jquery/index.php");
String parent=driver.getWindowHandle();
String child=driver.getWindowHandle();
driver).switchTo().window(child);
driver.findElement(By.name("name")).sendKeys("Abhishek Saxena");
To click on SUBMIT you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("input.button[value='Submit']")).click();
xpath:
driver.findElement(By.xpath("//input[#class='button' and #value='Submit']")).click();
However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.button[value='Submit']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='button' and #value='Submit']"))).click();
Reference
You can find a detailed discussion on NoSuchElementException in:
NoSuchElementException, Selenium unable to locate element

Categories