Scraping text() value if href contains a part of specific text - google-apps-script

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())
}
})

Related

Change phone number

I'm trying to understand how to access the phone number here:
<div class="address__location">
<p>
Siddals Road
<br> Derby DE1 2QD
</p>
<p>Main Phone:
0800123123
</p>
</div>
I need to find the html element of the phone number so I can replace it with another number.
So if I were to use getElementbyID"xxx" it would return the phone number.
There is no id attribute set on that element.
You could use another selector, though, by using document.querySelector and passing a[href^="tel:"]. This will search for a elements that have an href attribute that starts-with (that is what ^= means) the text tel:.
so
var telephoneNode = document.querySelector('a[href^="tel:"]');
telephoneNode.textContent = 'some other phone'; // change the displayed text
telephoneNode.href = 'some other url'; // change the href and in effect where the link points to

How to get text from xhtml

<div>
<fieldset>
<legend class="lheader">Section Information:</legend>
<span id="lblSectionInfo">
Name:
<font style="font-weight:normal">rr</font>
<br>
Type:
<font style="font-weight:normal">Section Type </font>
Section List:
<font style="font-weight:normal"> Yes </font>
Status:
<font style="font-weight:normal">
Section:
<font style="font-weight:normal">Section Condition</font>
<br>
</span>
</fieldset>
</div>
I have to get the text from the above Section Information section of the screen.
I tried getText() and it returns blank row, tried getAttribute("innerText") and it returns N/A, tried getAttribute("innerHTML") and it returns N/A
Not sure how I can get the complete text or individual test e.g. Section Name
should return text Section "RR" etc. The Xpath I am using is correct.
Any help is really appreciated.
Fyi, I am using Xpath below to get the text.
//div[#id = 'TestView5']//span[#id = 'lblSectionInfo']
My Xpath is correct as I am able to highlight the complete text on this section when use it by Selenium IDE.
It's quite difficult to separate those fields because they are under the same span node. I can see two workarounds.
1. Use index of <font> node as an anchor.
Name: "//span[#id='lblSectionInfo']/font[1]"
Type: "//span[#id='lblSectionInfo']/font[2]"
Section List: "//span[#id='lblSectionInfo']/font[3]"
2. Use JavaScript to find the text nodes to get field names, and then use Selenium to find the <font> node to get their values. Finally, map them together.
function getTextNode(rootNode) {
var nodes = rootNode.childNodes;
var fieldNames = [];
var count=0;
for (var i = 0; i < nodes.length; i++) {
if ((nodes[i].nodeType == Node.TEXT_NODE)) {
if(nodes[i].textContent.trim().indexOf(':')>0) {
let text = nodes[i].textContent.trim();
fieldNames[count] = text.substring(0,text.length-1);
}
}
}
return fieldNames;
}

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">

How to check attribute value with variable in selenium

<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");

AngularJs - how to support a textbox with hyperlinks

I'm new to Angular but I'm trying to implement a textbox that allows users to enter in links. I only want to support links, and otherwise I want to block all html from being presented as such. I could theoretically use something other than a textarea, but my requirements are that it must be bound to a variable in my scope (right now with ng-model) and I cannot accept html tags other than '< a >'
Here is my example plnkr
In the example, I would like the second seeded item to display as a link, blue and underlined. However, the third item should display as it is currently shown (without interpreting it as html).
HTML:
<textarea maxlength="160" ng-model="val.text"></textarea>
<div class="btn" ng-click="submit()">Submit</div>
<br><br>
<div ng-repeat="item in items">
{{display(item)}}
</div>
JS:
$scope.submit = function() {
if (!$scope.val.text) return
$scope.items.push($scope.val.text);
}
$scope.display = function(txt) {
return txt;
// something here? if txt contains <a> and </a> indicate
// that we should display as html
}