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();
Related
I am not able to close the below window on "https://www.makemytrip.com/" website:
I tried with alert, notification and child browser popup, but nothing had worked out.
Please anyone help on this?
The object(close button) you should have hit to close the pop up is inside another frame.
So switch to that iframe first and then try to hit the close button. See below the code to switch frame.
driver.switchTo().frame("id of the element");
To click on the element X as 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:
driver.get("https://www.makemytrip.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title^='notification-frame']")));
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("i.we_close"))).click();
Using xpath:
driver.get("https://www.makemytrip.com/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#title, 'notification-frame')]")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//i[#class='wewidgeticon we_close']"))).click();
Browser Snapshot:
References
You can find a couple of relevant discussions in:
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
Selenium: Can't click on a button within an iframe
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?
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).
HTML:
Apply Now
I have already tried the following:
driver.findElement(By.xpath("//a[text()='Apply Now']")).click();
driver.findElement(By.xpath("(.//[#href='Apply Now'])")).click();
driver.findElement(By.xpath("//a[#href='Apply Now']")).click();
driver.findElement(By.linkText("Apply Now")).click();
driver.findElement(By.className("Apply Now")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();
driver.findElement(By.xpath("//div[contains(#class,'btn btn-success btn-lg')]")).click();v
Try to click with webdriver wait till element is properly loaded in Dom and ready to receive click.
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[.='Apply Now']"))).click();
Option 1
Try By.CssSelector
Option 2
Not sure if you are checking if element is exists before clicking on it.if not then use wait.until.
If possible paste error you are getting here.
Hope this helps.
The desired element is a dynamic element so to locate and click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Apply Now"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.btn.btn-success.btn-lg[href*='associateregistration']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='btn btn-success btn-lg' and contains(#href, 'associateregistration')][text()='Apply Now']"))).click();
Try this please, maybe it will help
driver.findElement(By.id("select2-section-tn-container")).click();
I am using a reference, shown below, for the radio button.
<input id="checkmo" class="radio" type="radio" data-bind="attr: {'id':getCode()}, value: getCode(), checked: isChecked, click: selectPaymentMethod, visible: isRadioButtonVisible()" name="payment[method]" value="checkmo"/>
I have used the below code for this but it failed to click on radio button:
WebElement radioBtn = driver.findElement(By.id("checkmo"));
radioBtn.click();
The Radio Button with which you are trying to interact is a React element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.radio#checkmo"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='radio' and #id='checkmo']"))).click();
You may introduce the WebDriverWait which is explicit wait in Selenium.
Code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("checkmo"))).click();