Selenium element can not be found within iframe - java

I am trying to retrieve a JSON element, the problem is that in the source code it doesn't exist, but I can find it via inspect element.
I have tried with
C.driver.findElement(By.id("ticket-parsed"))
and via XPath
C.driver.findElement(By.xpath("//*[#id=\"ticket_parsed\"]"));
and I can't find it.
Also
C.driver.switchTo().frame("html5-frame");
System.out.println(C.driver.findElement(By.id("ticket_parsed")));
C.driver.switchTo().defaultContent();
i get
[[ChromeDriver: chrome on XP (1f75e50635f9dd5b9535a149a027a447)] -> id: ticket_parsed]
on
driver.switchTo().frame(0) or driver.switchTo().frame(1)
i get that the frame doesn't exists
and at last i tried
WebElement frame = C.driver.findElement(By.id("html5-frame"));
C.driver.switchTo().frame(frame.getAttribute("ticket_parsed"));
an i got a null pointer exception
Here's an image of the source:
what am I doing wrong?

Well!
The element #ticket-parsed is in iFrame. So, you can click it without getting into an iframe.
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
or
driver.switchTo().frame(frame_index);
In your case,
driver.switchTo().frame("html5-frame");
After switching into the iframe, you can click that element using either XPath or CSS.
C.driver.findElement(By.id("ticket-parsed"))
NOTE:
After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();

I didn't found a solution with my excising setup,but i did found a js command which gets the object correctly
document.getElementById("html5-frame").contentDocument.getElementById("ticket_parsed")
you can integrate js commands like this
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(*yourCommandHere*);
if you want to get the output of the command just add the word return before your command (in this specific situation it didn't work but in any other situation it did)
*TypeOfData* foo = js.executeScript(return *yourCommandHere*);
at last because of limited time i had to use unorthodox methods like taking screenshots and comparing the images if they are exactly the same
Thanks for the help

Related

Issue with HtmlUnitDriver's frame switch in Java / Issue with switching to a 'srcdoc' based iframe

I have a java program to scrape a web page, using selenium-server-standalone-3.9.1 jar.
I create org.openga.selenium.htmlunit.HtmlUnitDriver to fetch the page which contains 2 iframes inside. JavascriptEnabled is true.
I can access the page and parse ok without issues, but for some reason, switching to those frames, doesn't seem to work. Here is a snippet of the code.
driver.switchTo().defaultContent();
//The below line works fine without issues, so I'm assuming switching is succeeding.
//FYI if I give a bogus bad frame name here, it does raise an exception, which indicates that //the driver is recognizing the frame ok
driver.switchTo().frame(driver.findElement(By.id("xyz"))); //no exceptions raised
//Here, driver.getPageSource() here shows no content, just have an empty as follows
//<html>
// <head/>
// <body/>
//</html>
// So, I can't get any actually existing elements inside the iframe at all here..
// i.e. getElements() etc would return nothing..
driver.switchTo().defaultContent();
driver.getPageSource(); //shows the full page ok again..
I don't believe it's page loading issue as the full source dump from the driver does show the contents of those embedded iframes source ok. So, I don't think "wait" would help here.
What am I missing here?
Tried to use WebDriverWait to wait for frames to be available etc, no luck.

Actions Class in Selenium Webdriver

I am trying to perform the Control+A operation using the Actions class in Selenium using the following query:-
driver.get("https://jqueryui.com/datepicker/");
new Actions(driver).keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL)
.build().perform();
However , instead doing Control+A for the contents on the Webpage , it is performing the same operation in the URL bar. Could someone please let me know what is the error here.Moreover the issue what i see is the control stays in the URL bar and it doesnt come down to the Webpage.
I think there is an issue with pressing keys in selenium 3.0 which is reported here Actions sendKeys UnsupportedCommandException with geckodriver
You can try following alternative way to do that -
driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "a"))

iFrame within an iFrame

I have a program in which I am calling an iFrame within an iFrame however when I attempt to return to the first iFrame it can no longer find the elements.
I have tried both
base.getDriver().switchTo().frame("PopUpCont");
Add_Prefix_NewContact();
Add_FirstName_NewContact();
Add_MiddleName_NewContact();
Add_LastName_NewContact();
Add_Suffix_NewContact();
iFrameConvertion_Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().frame("PrimaryAcctPick");
Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().defaultContent();
Add_JobTitle_NewContact();
as well as
base.getDriver().switchTo().frame("PopUpCont");
Add_Prefix_NewContact();
Add_FirstName_NewContact();
Add_MiddleName_NewContact();
Add_LastName_NewContact();
Add_Suffix_NewContact();
iFrameConvertion_Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().frame("PrimaryAcctPick");
Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().frame("PopUpCont");
Add_JobTitle_NewContact();
however neither solution can find the next web element, so I don't believe I am returning to the first iFrame correctly. I know the web element works as I have commented out and tried that as well.
In case anyone cares to take a look I found the solution.
base.getDriver().switchTo().frame("PopUpCont");
Add_Prefix_NewContact();
Add_FirstName_NewContact();
Add_MiddleName_NewContact();
Add_LastName_NewContact();
Add_Suffix_NewContact();
iFrameConvertion_Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().frame("PrimaryAcctPick");
Add_PrimaryAcct_NewContact();
base.getDriver().switchTo().parentFrame();
Add_JobTitle_NewContact();
Add_Email_NewContact();
Parent Frame got me back to where I needed.

java selenium - hidden input value

First Stack post, so don't be harsh if I get it wrong.
Im using selenium and java for some automated testing. All was going well until I tried to set the value of a hidden input.
When using 'type' in the Firefox IDE, it works a treat, but when I use the line below, it doesn't play at all. It just fails.
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Can anyone point me in the right direction.
Many Thanks.
Edit:
WebDriver driver = new ChromeDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("$([name=startDate_submit]).attr('type', 'text');");
Thread.sleep(3000);
// This line doesnt like to run because its hidden
selenium.type("name=startDate_submit", "2015-09-25");
Should this be the way I do this? I just cannot get it working.
just try with this command,
driver.findElement(By.xpath("path")).sendKeys("value");
but make sure you have clicked that path before providing input value. come back if you still have any problem.
Try with available javascript commads like document.getElementById, then set the value.

Accessing drop down list- selenium webdriver & java

I am trying to write a few test cases for automation testing as a part of practice assignment in selenium webdriver using java.
The java version is 1.6 and selenium webdriver version is 2.39 while firefox browser version is 29.0.1.
I am trying to access the drop down titled CARSIZE in the following link:
http://www.carrentals.com/
I am not able to manipulate it.
I have tried the following code...
driver.get("http:\\www.carrentals.com/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Select dropdown= new Select(driver.findElement(By.xpath("//*[#name='carType']")));
dropdown.selectByValue("carsize-1");
With the above code, it seems that I am able to find the element(as no exception is thrown) but not change a value. When I try to change a value by SELECTBYVALUE method, I get an exception saying element not visible. Can someone help me?
The Html code for the above can be seen in firebug and just for information I have also tried using ID and name instead of XPath for the concerned select box but I get the same exception.
Try this:
Select dropdown= new Select(driver.findElement(By.xpath("//select[#id='cartype']")));
dropdown.selectByVisibleText("Small Cars");
Updated.
Try this code. It works for me.
String dropdownXpath = "//label[#for='cartype']/following-sibling::div[#role='listbox']";
WebElement textInDropDown = webDriver.findElement(By.xpath(dropdownXpath + "//div[#class='text']"));
textInDropDown.click();
webDriver.findElement(By.xpath(dropdownXpath)).sendKeys("Small Cars");
It locates text in dropdown element and then sends value which you want to select.

Categories