Unable to get text() node before a specific span using xpath - selenium

I'm trying to find the immediate preceding text of an element.
Below is the sample HTML code that I'm working on.
<span class= "platform">
::before
<br>
name
<br>
age
<span class = "highlight">
::before
"Negine"
</span>
</span>
Came up with an XPath:
//span[#class='platform']/text()[following-sibling::span[position() = 1]]
But the XPath is returning both text nodes where as it should return only 'age'.

Have written this text using your provided HTML in the comment section.
Try using this xpath - //span[#class="platform"]/text()[3]

Related

Selenium getting href link returning null

Getting href link from following html is always giving me null. I have been trying multiple ways to get this I cannot use xpath since xpath changes for every page.
<div class="form-group" id="idfb">
<label class="control-label">PDF </label> <a type="button" value="Download" href="./decisiondecisionForm-pdfContainer-filePdfDownload&id=6303"><i class="fa fa-download fa-2x xh-highlight" aria-hidden="true"></i></a><br>
</div>
I am trying to get href in following way
val element = driver.findElement(By.cssSelector("*[id^='id']"))
val link = element.getAttribute("href")
Is issue in aria-hidden attribute ?
Actually you want get the href attribute from the <a> tag, and your selector is not referring to it, but to the div tag.
The div tag doesn't have the href attribute, so that's why your code returns null.
So, instead you can try with the following value: By.cssSelector("div[id^='id'] > a")
Can you try this: Assuming div class="form-group" id="idfb">
is parent tag.
val element = driver.findElement(By.cssSelector("*[id^='id' a]"))

Selector to extract element value of type different tagName

There is String on my web page which looks as "#Account9 Hey Dude". #Account9 is a link, 'Hey Dude' is span. Please help me to create Selector to extract this "#Account9 Hey Dude".
note: Can find something like selenium "normalize-space" method.
#Account9
Hey Dude
To create a selector that extracts the inner text of the provided markup:
<div class=“XYZ123>
<span class="r-18u37iz">
<a href="/Account9" dir="ltr" role="link" data-focusable="true" class=“ABC123”>#Account9 </a>
</span>
</div>
<span class=“ABCxyz123”> :Hey Dude</span
you need to find the first parent of these elements (div class=“XYZ123 and span class=“ABCxyz123”) , specify CSS selector for it and call innerText property.
const targetText = await Selector('<parent_of_these_elements>').innerText;

How to get complete text inside page-footer from div

How to get complete text inside page-footer in the below case.
<div class="page-footer">
<span >Environment: </span>
<span > test</span>
<span >Version: </span>
<span > 0.1.56</span>
</div>
You can use this CSS_SELECTOR :
div.page-footer span
this will select all the span which are inside the div with class page-footer.
In case, you are using JAVA as a binding language :
String allText = driver.findElement(By.cssSelector("div.page-footer span")).getText();
Hope this helps.
You can use the below method to extract all the visible text
In Java,
getText() method will return the visible text of an element (i.e. not hidden by CSS)
string footerText=driver.findElement(By.className("page-footer")).getText();
In C#,
var footerText=_driver.FindElement(By.ClassName("page-footer")).Text;

Not able to identify custom checkbox element/Need relative xpath

I need relative xpath of the span element
<span class="icon"></span>
in selenium web driver.
<span class="custom-checkbox">
<input id="personalization1" name="personalization-terms-check" class="personalization-terms-check required" value="accept" type="checkbox">
<label for="personalization1">
<span class="icon"></span>
<span class="order-ready-text">yes! I double-checked all personalization entered above and i'm ready to order</span>
</label>
</span>
need relative xpath of the span element
<span class="icon">
in above html and I am getting relative path:
//*[#id="main"]/div[3]/div[4]/div[2]/div/span/label/span[1].
Please help me to get an relative xpath or another way to make an click on the span
NOTE:
<span class="icon">
It can be multiple so I need unique relative xpath.
You can wait for the element visibility before performing any action like below.
WebElement element=driver.findElement(By.xpath("//*[#name='personalization-terms-check']/following-sibling::label/span"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
element.click();
and you can use different xpath or id to locate the element. the above xpath map be unique if only one personalization term checkbox on the page.
Can you try with this xpath?
"//label[#for='personalization1']/span[1]"
Hope this helps. Thanks.
Try this:
Try this XPath -
//input[#id='personalization1']/label/span
Try this xpath to click on checkbox:
.//input[#id='personalization1']
or you can use this xpath
.//span[contains(text(),'yes! I dou')]

Any suggesstions to locate the below elements in Selenium

Example HTML below. I want to locate the elements which contains the text Person Attributes and Age.
<div id="ext-gen210" class="x-tool x-tool-toggle"></div>
<span id="ext-gen214" class="x-panel-header-text">Person Attributes</span>
</div>
<div id="ext-comp-1071" class=" DDView ListView" style="height: auto;">
<p class="dragItem "><i class="icon-Gen"></i>Age</p>
</div>
Note: I am looking for a solution without using xpath or id or className as they might change with every new release of my website.
I tried to locate them using
'name' --> By.name("Person Attributes") and By.name("Age") but both failed.
By.name would check for the name attribute. What you need is to check the text of an element using By.xpath:
By.xpath('//div[span/text() = "Person Attributes"]')
Or, you can also check that an id element starts with ext-gen:
By.xpath('//div[starts-with(#id, "ext-gen")]')