Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当类名以空格开头时使用Selenium解析html_Python_Selenium - Fatal编程技术网

Python 当类名以空格开头时使用Selenium解析html

Python 当类名以空格开头时使用Selenium解析html,python,selenium,Python,Selenium,我正在尝试使用Selenium和python解析此html代码中的文本: <div class=" card card_selected" data-id="3248846777"> <p>Hello World!</p> </div> 我得到这个错误: selenium.common.exceptions.InvalidSelectorException: Message: Given css selector

我正在尝试使用Selenium和python解析此html代码中的文本:

    <div class="  card card_selected" data-id="3248846777">
        <p>Hello World!</p>
    </div>
我得到这个错误:

selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression ".  card card_selected" is invalid: InvalidSelectorError: '.  card card_selected' is not a valid selector: ".  card card_selected"
当类名以空格开头时会发生此错误

我如何处理这个错误

browser.find_element_by_css_selector('[class|=" "]')
请参阅此处的更多详细信息:

CSS
[attribute |=“value”]
选择器

[attribute |=“value”]
选择器是 用于选择具有以开头的指定属性的元素 指定的值


可能更容易使用xpath contains()函数


请告诉我们完整的代码。
browser.find_element_by_css_selector('[class|=" "]')
//div[contains(@class,'card_selected')]