Reputation: 853
A webpage contains
<div class="divclass">
<ul>
<li>
<a href="www.abc1.com">"hello world 1"</a>
<img src="abc1.jpg">
</li>
<li>
<a href="www.abc2.com">"hello world 2"</a>
<img src="abc2.jpg">
</li>
</ul>
</div>
I am able to get data under div using
element = driver.find_element(class: "divclass")
element.text.split("\n")
But I want all links respective to the achieved data
I tried using
driver.find_elements(:css, "div.divclass a").map(&:text)
but failed. How can I get related links to the data?
Upvotes: 0
Views: 2132
Reputation: 853
In ruby
element = driver.find_elements(:xpath, "//*[@class='divclass']//a")
list = element.collect{|e| hash ={e.text => e.attribute("href")}}
will return corresponding links with data in array of hashes
Upvotes: 0
Reputation: 187
If you want to get the href attribute try the below code(I am not familiar with ruby so I am posting the code in Java).
List<WebElement> elements = driver.findElements(By.xpath("//*[@class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getAttribute("href"));
}
The xpath points to all the a tags under the div tag with class name =divclass. If you want to get the text of all the links, you can use the blow code:
List<WebElement> elements = driver.findElements(By.xpath("//*[@class='divclass']//a"));
for(WebElement webElement:elements){
System.out.println(webElement.getText());
}
Hope it helps.
Upvotes: 2