How to check attribute value with variable in selenium - html

<div class="aw-widgets-cellListCellTitleBlock">
<h3 title="block1" class="aw-widgets-cellListCellTitle" id="CellTitle">block1</h3>
<label class="aw-widgets-cellListCellItemType aw-base-small">000027</label>
</div>
In given snippet title="block1" i want to take it in the form of variable foe
e.g. String sample="block1" and then it used as title=sample or //div[text()=sample].
I tried this one but its not working. Did you have any solution for it?

If you want to get the title value from HTML code, then you can use any one from the following code.
WebElement element = driver.findElement(By.xpath("//h3[contains(text(),'block1')]"));
or
WebElement element = driver.findElement(By.xpath("//h3[#id='CellTitle']"));
or
WebElement element = driver.findElement(By.xpath("//div[#class='aw-widgets-cellListCellTitleBlock']/h3"));
//get text
String text = element.getAttribute("title");

Related

Scraping text() value if href contains a part of specific text

I'm trying to collect the text in a if href contains venue/, so I tried to do it this way:
var venue = $('.details > span > a:contains(href="venue/")');
sheet.getRange(3,17).setValue(venue.text().trim());
But returns with no value, how should I be able to retrieve such value?
As the site changes the positions of the elements from time to time, I need to define this contains.
Expected Result:
Estadio Manuel Ferreira (Asunción)
Map Example:
<div class="details ">
11/08/2021
<span class="divider"></span>
CONMEBOL Libertadores
<span class="divider"></span>
<span>KO</span>
<span>
19:15
</span>
<br>
<span>Venue</span>
<span>
Estadio Manuel Ferreira (Asunción)</span>
</div>
Link to site:
https://int.soccerway.com/matches/2021/08/12/south-america/copa-libertadores/club-olimpia/clube-de-regatas-de-flamengo/3579565/
It seems like the issue is right on the first line, as the “venue” variable does not return what you expect.
I propose you select the anchor you are looking for by getting the last element of type a in the div you provided and assign the value of its href attribute to a variable called venue. After that, check if the venue variable is equal to venue/. If the condition returns true, get the anchor’s inner text, assign it to a variable called result and log it.
You can make it work by using the following code:
let element = $('.details a').last()
let venue = element.attr('href');
if (venue === 'venue/') {
let result = element.text()
console.log(result) // this is the value you are looking for
}
Updated:
let elements = $('.details a')
elements.each((index, value) => {
let href = $(value).attr('href')
if (href === 'venue/') {
console.log($(value).text())
}
})

Accessing the text of a class that contains other elements using Cheerio

I only want to access h1's text (H1 title is here in this case), but it prints everything. I tried adding .remove('.small-title') before text(), but it didn't work.
<div class="modal-know>
<h1>
H1 title is here
<div class="small-title">
Click
Click 2
</div>
</h1>
</div>
Node.js code
var newsTitle = $2('.modal-know h1').text(); // prints out everything
console.log(newsTitle);
have a look at cheerio docs: text()
it says
including their descendants
That is the same behaviour that jQuery .text()
So maybe this answer could help you :jQuery: using .text() to retrieve only text not nested in child tags
Here you have the code I tested:
let newsTitle = $('.modal-know h1').contents()[0].nodeValue;
// solution 2:
// .clone() //clone the element
// .children() //select all the children
// .remove() //remove all the children
// .end() //again go back to selected element
// .text(); // prints out everything
//solution 3:
// .contents().filter(function(){
// return this.nodeType == 3;
// })[0].nodeValue;
console.log(newsTitle);
*in your code sample ther is a missing " in the div modal-know class
<div class="modal-know> -> <div class="modal-know">

Selenium test (selection of element having no attribute)

<! DOCTYPE html>
<html>
<head>Sample</head>
<body>
<div class="panelBody">
<div class=panel-section></div>
<div class=panel-section style="display:block"></div>
</div>
</body>
</html>
In given Snippet there are two elements with same class. I have to select the element which does not having style attribute.If i tried to search with panel-section class its giving ambiguity error.So how to select div element which does not having style attribute.i.e
<div class=panel-section></div>
Try this:
//div[#class='panelBody']/div[not(#style)]
Explanation: First find the div with class panelBody, then find child div elements in the panelBody div which doesn't contain #style attribute.
Use findElements method if there are more than one div element without #style attribute, otherwise findElement() method would suffice.
Since there are more than one elements with same class name, you need to use Selenium's driver.findElements() method. I have tried getting this element, but I wonder if it is clickable. Only element can actually be useful here is text Sample.
Check below code. Let me know if it is similar to what you are looking for.
List<WebElement> linksize=null;
String links[]=null;
linksize = driver.findElements(By.cssSelector("div[class=panel-section]"));
int linksCount = linksize.size();
links= new String[linksCount];
for(int i=0;i<linksCount;i++)
{
links[i] = linksize.get(i).getAttribute("style");
if(links[i].isEmpty())
{
System.out.println("I am div without style");
linksize.get(i).click();
}
}

Searching in html on the behalf of ID

Is searching possible in html tags on the behalf of ID? for example to find div tag having id="abc".
I can use document.getElementByID("abc"). But i need parent div + its inner HTML in return of searching. i.e if this div has childs
Try this :-
<script >
function showHTML(){
var vinner=document.getElementByID("abc").innerHTML;
var totalinner="<div >"+vinner+"</div>";
alert(totalinner);
}
</script>
HTML part:-
<body onload="showHTML();">
<div id="abc">
Hello inside abc
<div>
Inner div inside abc tag.
</div>
</div>
</body>
Its working fine. You can get Attributes here.
It's hard to understand what you want to achieve:
document.getElementById("abc").parentNode.innerHTML;
//will return <div id="abc"> and other items from parrent
document.getElementById("abc").getAttribute("name");
//will atribute of <div id="abc">
if (document.getElementById("abc").hasChildNodes()) {
// It has at least one
}
Using jQuery is much simplier, you could do that:
$("#abc").attr('id') //retunrs id
$("#abc").attr('class') //returns classes
//or other manipulations
One way to do this is to use outerHTML, which:
gets the serialized HTML fragment describing the element including its descendants.
Given the following HTML:
<div id="abc" data-attr="A custom data-* attribute">Some text in the div.</div>
The following JavaScript will log, in the console, the HTML of the element of id equal to abc:
var htmlString = document.getElementById('abc').outerHTML;
console.log(htmlString);
JS Fiddle demo.
References:
outerHTML.
outerHTML compatibility.

Regular expression for selecting attributes name only from within certain tags

What's the regex which allows me to select all the attribute names from <form> and <input> tags but not from any other HTML tag?
For example:
<!-- all attribute names get selected -->
<input class="something" id="yes" type="text" name="my-field" value="Hello, world!">
<!-- class and id don't get selected because it's a div -->
<div class="something" id="no"></div>
<!-- class gets selected -->
<form class="my-form"></form>
I'm only after the attribute names
Such a regexp would be very complicated to build. Despite the fact that you can't match all HTML by regexes, it would need a very complicated lookbehind to check whether the attribute name which you want to match comes after a opening tag whose name is either "form" or "input". Don't try to build such a regex, you'd go crazy and/or end up with an unreadable, non-maintainable or -undestandable monster.
Instead, use a DOM parser (there will be one for your language) and apply DOM selectors and get the attribute names of the elements.
It is not easy task to do it with regex and actually it is not a good idea to do it with regex. But it is possible >>
input = '...';
var tmp = input, found, params = [];
var re = /(<(?:form|input)\b.*?\s)([\w\-]+)=(['"]).*?\3/gi;
do {
found = 0;
tmp = tmp.replace(re, function($0,$1,$2,$3) {
params.push($2);
found = 1;
return $1;
});
} while (found);
Check this demo.