Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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通过css选择器获取元素_Python_Css_Selenium_Selenium Webdriver_Web Scraping - Fatal编程技术网

Python selenium通过css选择器获取元素

Python selenium通过css选择器获取元素,python,css,selenium,selenium-webdriver,web-scraping,Python,Css,Selenium,Selenium Webdriver,Web Scraping,我正试图从给定的每个块中获取用户详细信息 driver.get("https://www.facebook.com/public/karim-pathan") wait = WebDriverWait(driver, 10) li_link = [] for s in driver.find_elements_by_class_name('clearfix'): print s print s.find_element_by_css_selector('_8o.

我正试图从给定的每个块中获取用户详细信息

driver.get("https://www.facebook.com/public/karim-pathan")         
wait = WebDriverWait(driver, 10)
li_link = []
for s in driver.find_elements_by_class_name('clearfix'):
    print s
    print s.find_element_by_css_selector('_8o._8r.lfloat._ohe').get_attribute('href')
    print s.find_element_by_tag_name('img').get_attribute('src')
它说:

无法使用css选择器找到元素


任何明显的提示。

由于您使用的是元素应用的所有类名,因此在CSS选择器的开头添加一个
应该可以修复它

试试这个:

s.find_element_by_css_selector('._8o._8r.lfloat._ohe')
而不是:

s.find_element_by_css_selector('_8o._8r.lfloat._ohe')

由于您使用的是元素应用的所有类名,因此在CSS选择器的开头添加一个
应该可以修复它

试试这个:

s.find_element_by_css_selector('._8o._8r.lfloat._ohe')
而不是:

s.find_element_by_css_selector('_8o._8r.lfloat._ohe')

@leo.fcx指出的关于选择器的内容:


@leo.fcx指出的关于选择器的内容:


仅仅是基于假设您未登录的猜测。您将获得所有类clearfix的异常原因,元素
。\u 8r.lfloat.\u ohe
不存在。因此,您的代码没有达到所需的元素。无论如何,如果您试图获取href和img结果源,您不需要像
@leo.fcx
所建议的那样迭代所有clearfix原因,您的css是不正确的,尝试leo提供的css,您可以获得所需的结果,如下所示:

driver.get("https://www.facebook.com/public/karim-pathan")         
for s in driver.find_elements_by_css_selector('._8o._8r.lfloat._ohe'): // there didn't seemed to iterate over each class of clearfix
    print s.get_attribute('href')
    print s.find_element_by_tag_name('img').get_attribute('src')

另外,对于任何语法,我都很抱歉,从来没有研究过python绑定:)

仅仅是基于您未登录的假设的猜测。您将获得所有类clearfix的异常原因,元素
。\u 8r.lfloat.\u ohe
不存在。因此,您的代码没有达到所需的元素。无论如何,如果您试图获取href和img结果源,您不需要像
@leo.fcx
所建议的那样迭代所有clearfix原因,您的css是不正确的,尝试leo提供的css,您可以获得所需的结果,如下所示:

driver.get("https://www.facebook.com/public/karim-pathan")         
for s in driver.find_elements_by_css_selector('._8o._8r.lfloat._ohe'): // there didn't seemed to iterate over each class of clearfix
    print s.get_attribute('href')
    print s.find_element_by_tag_name('img').get_attribute('src')

另外,对于任何语法,我都很抱歉,从来没有研究过python绑定:)

添加了它,仍然没有change@nlper好的,你能试着按标签名定位每个搜索结果中的链接吗
s.find\u element\u by\u tag\u name(“a”)
。否,它给出了
无法找到标记名为“a”的元素的提示。
@alecxe@nlper好的,谢谢,你使用哪种浏览器?另外,你有“登录facebook”的步骤吗?添加了,仍然没有change@nlper好的,你能试着按标签名定位每个搜索结果中的链接吗
s.find\u element\u by\u tag\u name(“a”)
。否,它给出了
无法找到标记名为“a”的元素的提示。
@alecxe@nlper好的,谢谢,你使用哪种浏览器?另外,您是否有“登录facebook”步骤?