Getting the text from the element in iframe with Selenium, Java - java

I am trying to get the text in the element, which is within an iframe. How can i get the right Xpath or CSS- Selector? I have switched to the frame, but the compiler is unable to locate the element with xpath or cssSelector. Probably the problem is that I am contentiously falling to write the right path and selector. All experiments with right click- copy the path or writing the path by myself have no result.
Here is my code to switch to the iframe and get the text.
driver.switchTo().frame("iframe-analytics");
Thread.sleep(10000);
String st=driver.findElement(By.xpath("//div[#id='container']/div/div[2]/span")).getText();
System.out.println(st);
here is the html for the element and the element itself
html
the element

finally this works for me
String st=obj.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/span")).getText();
System.out.println(st);

Related

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

How to get unique xpath for all the hyperlink dynamically in webpage

I have some web page which contains hyperlinks for some word, which also can be repeated. I need to verify the hyperlink for each word, e.g. the word headache which is 3 times in that page. After clicking on each headache the same page opens.
Is there any method which can return the XPath for all words headache because for the automation i need to click on every headache and verify the URL.
I could write the XPath manually but what if there are a lot of headache words on same page which have the hyperlink?
In xpath you have to declare type of element for example:
driver.findElements(By.xpath(".//link[text()='headache']")).forEach(WebElement::click)
Also if you need just to check the link is correct you can use Rest Assured library and send get requests by http.
You can try this xpath = //a[text()='headache'] or find with linkText driver.findElements(By.linkText("headache"))
List<Webelement> ele = driver.findElements(By.linkText("headache"))
for(Webelement link: ele){
link.click();
// other operation
}

How to get to the element through JS in iframe? Selenium, Java

So what we have - there's an iFrame on page which shows a preview of previously generated data. I need to get element with text and then get CSS value from it.
Take a look at the DOM here:
example of the page
xpath to this element looks right to needed element (God save Chrome DevTools!).
But script cannot detect this element.
What I've did:
1 - switched to Iframe where my element is located - successfully.(no more iframe inside this iframe as per screenshot).
2 - tried to find element - NoSuchElementException.
Probably, such issue appears because of this #document thing ?If so - how do I can solve it and get to needed element with script ?
may be you need to wait till the frame to be loaded as given below.
WebDriverWait wait=new WebDriverWait(driver, 90);
driver=wait.untill(ExpectedConditions.frameToBeAvailableAndSwitchToIt("previewFrame");
WebElement element=driver.findElement(By.TagName("em"));
String fontStyle=element.getCssValue("font-sytle");
System.out.println(fontStyle);

How can I get text from element with Selenium webdriver and Java?

My code:
WebDriver driver = new SafariDriver();
driver.get("http://bet.hkjc.com/football/default.aspx");
WebElement matchs = driver.findElement(By.cssSelector("span.Head to Head"));
System.out.println(matchs);
driver.quit();
How can I crawl Manchester Utd and Celta Vigo?
WebElement matchs = driver.findElement(By.xpath("//a[#title='Head to Head']"));
System.out.println(matchs.getText());
Use firebug and firepath addons in firefox and inspect that element and get the xpath and put it here inside double quotes in this code :
System.out.println(driver.findElement(By.xpath("")).getText());
If you don't know how to use firebug and firepath refer this link
You can locate the element either by css selector or xpath selector
By using xpath
driver.findElement(By.xpath("//a[#title='Head to Head']"));
By using css Selector
driver.findElement(By.cssSelector("span > a[title='Head to Head']"));
OR Try somethings like this if not getting the match
driver.findElement(By.cssSelector("td.cteams.ttgR2>span>a[title='Head to Head']"));
Note : in your code you are trying like span.Head to Head in CSS selector . dot represents the class and according to your path you are locating span tag which have class name "Head to Head" which doesn't exist in your dom as this is the title of anchor tag.
Went through the Firebug and Firepath plugins of Firefox initially to get the Xpath or css path
Explore some blogs to get clear understanding, you will be able to create by yourself
Refer This link for the same
I assume all the above answers doesn't work for you and am providing another answer.
I can see both the texts are under "a" tag. So the idea is to navigate to the element and use getText() - which returns the visible text.
String word = driver.findElement(By.xpath("//span/a")).getText();
System.out.println(word);
Hope this works for you.
In all of my tests I'm using the getAttribute like this to get text and it is working fine for me on all drivers :
assertEquals(strCity, txtCity.getAttribute("value"));

XPATH issue. Not able to read parts of html

I need to able to click on a link (I am using Selenium and Java). I am searching the link using xpath but for some reason I am not getting most of the webpage, just a bunch of white spaces. In the image you can see the highlighted link I am looking for.
I tried:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());
System.out.println(driver.findElement(By.xpath("//*[#id='li-7']/div/a")).getText());
I get: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element
If I do:
System.out.println(driver.findElement(By.xpath("//*")).getText());
I only get a few elements from the page and bunch of white spaces. What could be wrong?
Please help. I couldn't fit in the entire html source to show you. I hope that's ok.
html source pic
In case the element is inside an iframe, you can get it by switching to iframe then call findElement. See code below:
WebElement iframeElement = driver.findElement(By.id("id_of_the_iframe"));
driver.switchTo().frame(iframeElement);
Then you can find the element with your xpath:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());

Categories

Resources