简体   繁体   English

可以使用 Selenium Python 查找类名

[英]Can find Class Name using Selenium Python

Below is a snippet of the HTML using page_source method in selenium.下面是在 selenium 中使用page_source方法的 HTML 片段。

Why can't I find className cell-content cell-colshopCapacity ?为什么我找不到 className cell-content cell-colshopCapacity

'<div unselectable="on" class="x-grid-cell-inner" style="text-align:left;">Daily</div></td><td class="x-grid-cell x-grid-td x-grid-cell-templatecolumn-1065 x-grid-cell-last x-unselectable "><div unselectable="on" class="x-grid-cell-inner" style="text-align:left;"><span class="cell-content cell-colshopCapacity">101</span></div></td></tr></tbody></table></div></div></div><div class="x-container save x-container-default x-box-layout-ct" style="padding: 5px 15px 18px 9px; width: 907px;"

Using the below returns an empty list []使用以下返回一个空列表[]

from selenium import webdriver

driver = webdriver.Chrome(r'path/to/chromedriver')
driver.find_elements_by_class_name('cell-content cell-colshopCapacity')

If I use driver.page_source in vs code it returns the HTML from the webpage which is how I was able to produce the above snippet.如果我在 vs 代码中使用driver.page_source ,它会从网页返回 HTML,这就是我能够生成上述代码段的方式。 Am I correct in assuming if driver.page_source returns HTML with the said class name then I should be able to find it using the below, given that if it was in another frame then it wouldn't return the class name I am trying to find?我是否正确假设driver.page_source返回带有所述类名的 HTML,那么我应该能够使用下面的方法找到它,因为如果它在另一个frame那么它不会返回我试图找到的类名? Or do I have that assumption wrong?还是我的假设是错误的?

driver.find_elements_by_class_name('cell-content cell-colshopCapacity')

class name do not work with spaces in Selenium.类名不适用于 Selenium 中的空格。

so try changing that to css_selector instead :所以尝试将其更改为css_selector

driver.find_elements_by_css_selector('span.cell-content.cell-colshopCapacity')

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

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