How to get spans text without inner attributes text with selenium?

2024/9/22 5:24:08
<span class="cname"><em class="multiple">2017</em> Ford 
</span>
<span class="cname">Toyota
</span>

I want to get only "FORD" and TOYOTA in span.

test.find_element_by_class_name('cname').text

return "2017 FORD" and "TOYOTA". So how can i get particular text of span?

Answer

Pure XPath solution:

//span[@class='cname']//text()[not(parent::em[@class='multiple'])]

And if you alse want to filter white-space-only text-nodes():

//span[@class='cname']//text()[not(parent::em[@class='multiple']) and not(normalize-space()='')]

Both return text-nodes not an element. So Selenium will probably fail. Take a look here: https://sqa.stackexchange.com/a/33097 on how to get a text-node(). Otherwise use this answer: https://stackoverflow.com/a/67518169/3710053

EDIT:

Another way to go is this XPath:

//span[@class='cname']

And then use this code python-example to get only direct text()-nodes.

EDIT 2

all_text = driver.find_element_by_xpath("//span[@class='cname']").text
child_text = driver.find_element_by_xpath("//span[@class='cname']/em[@class='multiple']").textparent_text = all_text.replace(child_text, '')
https://en.xdnf.cn/q/119627.html

Related Q&A

List of 2D arrays with different size into 3D array [duplicate]

This question already has answers here:How do you create a (sometimes) ragged array of arrays in Numpy?(2 answers)Closed last year.I have a program that generating 2D arrays with different number of r…

How can I read data from database and show it in a PyQt table

I am trying to load data from database that I added to the database through this code PyQt integration with Sqlalchemy .I want the data from the database to be displayed into a table.I have tried this …

Python: Cubic Spline Regression for a time series data

I have the data as shown below. I want to find a CUBIC SPLINE curve that fits the entire data set (link to sample data). Things Ive tried so far:Ive gone through scipys Cubic Spline Functions, but all …

python CSV , find max and print the information

My aim is to find the max of the individual column and print out the information. But there is problem when I print some of the information. For example CSIT135, nothing was printed out. CSIT121 only p…

Error on python3 on windows subsystem for linux for fenics program

Im just starting to use fenics in python3 on windows subsystem ubuntu, and when I open the first titurial file I got this error. Solving linear variational problem. Traceback (most recent call last): …

python regex: how to remove hex dec characters from string [duplicate]

This question already has answers here:What does a leading `\x` mean in a Python string `\xaa`(2 answers)Closed 8 years ago.text="\xe2\x80\x94" print re.sub(r(\\(?<=\\)x[a-z0-9]{2})+,&quo…

Iterating through list and getting even and odd numbers

yet one more exercise that I seem to have a problem with. Id say Ive got it right, but Python knows better. The body of the task is:Write a function that takes a list or tuple of numbers. Return a two-…

Cannot import tensorflow-gpu

I have tried to import tensorflow-gpu and Im getting the same error with different versions of CUDA and cuDNN. My GPU is compatible with CUDA and I have no problems installing but when I try to import …

comparing two Dataframe columns to check if they have same value in python

I have two dataframes,new1.Name city0 sri won chn1 pechi won pune2 Ram won mum0 pec won keralanew3req 0 pec 1 mutI tried, mask=new1.Name.str.contains("|".join(…

Input gravity forms entries in a database locally stores (created with python)

I hope you are all doing alright. Is it possible to connect a gform entry to a database created with Python and stored in my PC with a little variation of the following code? add_action("gform_af…