简体   繁体   English

Python Selenium xpath空格

[英]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; 浏览互联网和其他Stackoverflow帖子后; I think the problem is that there are white spaces in @class , and it just doesn't work. 我认为问题是@class中有空格,它只是不起作用。 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 . 然后我找到了解决方法,以另一种方式做到这一点,但问题是,在第一堂课后,我也想去第二课,这是我知道我可以用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. 在这张图片中,'text'是它应该废弃的用户名之一。

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. 尝试在您的情况下使用contains以避免空格。 For example: 例如:

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

Contains will skip spaces. 包含将跳过空格。

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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