简体   繁体   中英

Python Selenium xpath white spaces

I would like to get usernames from one page, but for some reason I just can't get it working..

After browsing internet and other Stackoverflow posts; I think the problem is that there are white spaces in @class , and it just doesn't work. Then I found solution to do it the other way, but the problem is that after first class, I would like to go to second class as well, and this is the only way I know I can do it with find_elements_by_xpath .

Picture to inspect element of what I want to get

In this picture, 'text' is one of the usernames it should scrap.

My code:

usernames = driver.find_elements_by_xpath("//a[@class='kik-widget card card-just-text card-with-shadow']//h3[@class='kik-widget-text-muted']")
usernames2 = [x.text for x in usernames]
print(usernames2)

Any help much appreciated.

Try to use contains in your case to avoid spaces. For example:

//h3[contains(@class, 'kik-widget-text-muted')

Contains will skip spaces.

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM