Unable to use elements within iframe after switching - selenium

I am using switching to an iframe using the below statements but on the final wait I get web element not found. Does anyone have any suggestions? I have trolled through google for about a day trying different techniques. None have worked so far
WebDriver driver = DriverFactory.getWebDriver()
WebDriverWait wait = new WebDriverWait(driver, 15)
driver.switchTo().defaultContent()
WebElement iframe = driver.findElement(By.cssSelector(".b-iframe__iframe"))
driver.switchTo().frame(iframe)
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(
'fieldset.radio-switch-group')))
Screen shot of HTML below image

As per the HTML you have shared to locate the desired WebElement you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be visible and you can use either of the following solutions:
Using cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.b-iframe__iframe[src*='securetest']")));
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("fieldset.radio-switch-group#balance-type")));
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#class='b-iframe__iframe' and contains(#src,'securetest')]")));
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//fieldset[#class='radio-switch-group' and #id='balance-type']")));
Here you can find a relevant discussion on Ways to deal with #document under iframe

Related

Amazon click image based on search

I am very new to selenium and tried this and it didn't work. You can easily reproduce this by going to amazon site and search for hairclip and you will find this image in the search. Once this image is found, i want to go to next page but it is not happening.
System.setProperty("webdriver.chrome.driver", "C:\\software\\chromedriver_win32\\chromedriver.exe");
ChromeDriver Driver = new ChromeDriver();
Driver.get("http://www.amazon.com");
Driver.manage().window().maximize();
Driver.findElement(By.id("twotabsearchtextbox")).sendKeys("Hairclip");
Driver.findElementById("nav-search-submit-button").click();
By by = By.xpath("//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')]");
WebDriverWait w = new WebDriverWait(Driver, 20);
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by));
element.click();
Error as below
Expected condition failed: waiting for element to be clickable:
By.xpath:
//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL.AC_UL320.jpg')]
(tried for 20 second(s) with 500 milliseconds interval)
I appreciate your time for the reply and effort.
Looks like you wanna click on first image, in that case this xpath should work :
//img[#data-image-index='1']
try it like :
WebElement element = w.until(ExpectedConditions.elementToBeClickable(by.xpath("//img[#data-image-index='1']")));
element.click();
The XPath locator you are using //img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')] matches 4 elements on that page.
To click the element you want please use this locator:
(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]
So the code line will be
By by = By.xpath("(//img[contains(#src,'https://m.media-amazon.com/images/I/716AFuiNFoL._AC_UL320_.jpg')])[last()]");

Caused by: org.openqa.selenium.ElementClickInterceptedException: element click intercepted: [duplicate]

I have a project that I am working on with java and selenium.
the test work OK in UI mode.
However in headless mode I get this error
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
how can I resolve this issue (working in UI mode). this is my code
WebDriver driver = getWebDriver();
WebElement element;
Thread.sleep(60000);
element = driver.findElement(By.xpath("//label[#formcontrolname='reportingDealPermission']"));
element.click();
why in selenium there is no operation to move to the element and break all layers.
this is the UI.
this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue
This error message...
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
...implies that the click on the desired element was intercepted by some other element.
Clicking an element
Ideally, while invoking click() on any element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
Update
After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:
Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser Viewport:
Adding the argument start-maximized
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
Adding the argument --window-size
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1400,600");
WebDriver driver = new ChromeDriver(options);
Using setSize()
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1440, 900));
You can find a detailed discussion in Not able to maximize Chrome Window in headless mode
Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions invisibilityOfElementLocated before attempting the click() as follows:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[#class='footer']")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
References
You can find a couple of related relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
Try adding an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
and if this doesn't work then try using the JS Executor
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
None of the above answers worked for me. Try using action class as follows:
WebElement element = driver.findElement(By.xpath("//div[#class='footer']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
For this issue:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562).
Another element receives the click:
Answer is to explicitly wait with javascript executor. This combination is working for me:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver, 10); ---> has deprecated and it gives an error. Please use below instead:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
Ensure to import relevant items for the WebDriverWait and ExpectedConditions.
In my case "JavaScript" works:
WebElement ele = driver.findElement(By.xpath("(//input[#name='btnK'])[2]"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", ele);
It was taken from:
http://makeseleniumeasy.com/2020/05/25/elementclickinterceptedexception-element-click-intercepted-not-clickable-at-point-other-element-would-receive-the-click/
Answer is worked for me.
Please check the below sreenshot for the my problem reference.
Below alert is comes in between the the my button and frame.
enter image description here
In my case, nothing worked. Only the THREAD.SLEEP() method worked!

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']")))

Selenium how to click/ access an a tag with a href that has a javascript function

I have searched but havent found something similar to what Im trying to do. I'm using java by the way, I'm trying to click/access an a tag with selenium. The issue is that I'm not sure how to go about it. There seems to be a function/event that I need to set off but not quite sure how to. I tried a few ways as get text and clicking but I knew that wasn't going to work. Also I seen there are ways of using JavascriptExecutor but not sure how to use it for my case. I will post the tag below and alsothe function signature, that might help. If theres a similar question please post the link.
<a name="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" id="DERIVED_SSS_SCL_SSS_ENRL_CART$276$" ptlinktgt="pt_peoplecode" tabindex="203" onclick="javascript:cancelBubble(event);" href="javascript:submitAction_win0(document.win0,'DERIVED_SSS_SCL_SSS_ENRL_CART$276$');" class="SSSHYPERLINKBOLDSMALL">Enrollment Shopping Cart</a>
the signature
function submitAction_win0(form, id, event)
You don't need any JS. Just use this xpath:
"//a[contains(#onclick,'javascript:cancelBubble(event);')]"
Be sure the element is clickable, see
import org.openqa.selenium.support.ui.ExpectedConditions;
for the case of more matches:
List<WebElement> elements = driver.findElements(By.xpath("//a[contains(#onclick,'javascript:cancelBubble(event);')]"));
int elementIndex = 0; // 0 to get first of the 33 mathes, 32 to get the last one
WebElement element = elements.get(elementIndex);
element.click();
EDIT:
You should use WebDriverWait to avoid NoSuchElementException this way the driver will wait until the element is clickable... it will wait up to 10 seconds you can tell it to wait more if needed...
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Student Center")));
element.click();
Using click():
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
element.click();
Using JavascriptExecutor (Not recommended):
WebElement element = driver.findElement(By.cssSelector("a[class='SSSHYPERLINKBOLDSMALL']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
You can use other locators too... like linkText:
WebElement element = driver.findElement(By.linkText("Enrollment Shopping Cart"));
Or partialLinkText:
WebElement element = driver.findElement(By.partialLinkText("Shopping Cart"));

PartialLink text for selecting value in selenium

Scenario: Search google with word 'Selenium' and click on the first link.
I have written this below code:
`WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
List<WebElement> alist = driver.findElements(By.partialLinkText("Selenium"));
System.out.println(alist.size());`
but it is giving me size as zero. Why?
Actually when you are going to find list of all links with partial text Selenium, all links are not present in the DOM due to fast execution. You should try using WebDriver to wait until all elements visible in the DOM as below :-
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> alist = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.partialLinkText("Selenium")))
System.out.println(alist.size());
Output:-13
Hope it helps...:)
ur problem occurs because u don't wait after searching. just wait a little moment so that browser can search and show the result. use the below code :
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
Thread.sleep(5000);
though this kind of wait is not recommended to use. Try to use wait by using until like:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("element css path")));
u can use by xpath or id or class here.