Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

How to get the correct css selector for selenium (python)

I try to automate some stuff. If I identify the css selector with selenium IDE I get the correct address most of the time. If I use the inspect mode of chrome or firefox developers edition, I don't. I do not understand why that is, or how to fix this.

Also is there a possiblity to check the

data-testid="801212545-follow" 

if it contains "follow"?

I used the following:

elements = driver.find_elements(By.CSS_SELECTOR, ".css-1dbjc4n:nth-child(1) > .css-18t94o4:nth-child(1) > .css-901oao > .css-901oao > .css-901oao")
for elem in elements:
   print(f"{elem}")

To get

<div class="css-1dbjc4n" data-testid="placementTracking"><div class="css-1dbjc4n r-15d164r" style="min-width: 94px;"><div role="button" data-focusable="true" tabindex="0" class="css-18t94o4 css-1dbjc4n r-urgr8i r-42olwf r-sdzlij r-1phboty r-rs99b7 r-1w2pmg r-1vuscfd r-1dhvaqw r-1ny4l3l r-1fneopy r-o7ynqc r-6416eg r-lrvibr" data-testid="801212545-unfollow"><div dir="auto" class="css-901oao r-1awozwy r-jwli3a r-6koalj r-18u37iz r-16y2uox r-1qd0xha r-a023e6 r-b88u0q r-1777fci r-eljoum r-dnmrzs r-bcqeeo r-q4m81j r-qvutc0"><span class="css-901oao css-16my406 css-bfa6kz r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0"><span class="css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0">Folge ich</span></span></div></div></div></div>

There is "Folge", but the script returns:

<selenium.webdriver.remote.webelement.WebElement (session="bc2ab192ac38c886f3c48888e8cce1bf", element="9fbeadc8-bbae-42cc-a5b4-e5d38f2c7f87")>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To identify the element:

data-testid="801212545-follow" 

You can use the following :

[data-testid$='follow']

Where, the expression data-testid$='follow' checks for the value of data-testid attribute to end with follow


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...