[英]Extract CSS Selector for an element with Selenium
For my project I need to extract the CSS Selectors for a given element that I will find through parsing. 对于我的项目,我需要为将通过解析找到的给定元素提取CSS选择器。 What I do is navigate to a page with selenium and then with python-beautiful soup I parse the page and find if there are any elements that I need the CSS Selector of.
我要做的是导航到包含硒的页面,然后使用python-beautiful汤解析页面,并查找是否需要CSS选择器的任何元素。 For example I may try to find any input tags with id "print".
例如,我可能尝试查找ID为“ print”的所有输入标签。
soup.find_all('input', {'id': 'print')})
If I manage to find such an element I want to fetch its extract it's CSS Selector, something like "input#print". 如果我设法找到一个这样的元素,我想获取它的摘录是CSS Selector,类似“ input#print”。 I don't just find using id's but also a combination of classes and regular expressions.
我不仅找到使用id的方法,而且还找到了类和正则表达式的组合。 Is there any way to achieve this?
有什么办法可以做到这一点?
Try this. 尝试这个。
from scrapy.selector import Selector
from selenium import webdriver
link = "https://example.com"
xpath_desire = "normalize-space(//input[@id = 'print'])"
path1 = "./chromedriver"
driver = webdriver.Chrome(executable_path=path1)
driver.get(link)
temp_test = driver.find_element_by_css_selector("body")
elem = temp_test.get_attribute('innerHTML')
value = Selector(text=elem).xpath(xpath_desire).extract()[0]
print(value)
Ok, I am totally new to Python so i am sure that there is a better answer for this, but here's my two cents :) 好的,我对Python完全陌生,因此我确信对此有更好的答案,但这是我的两分钱:)
import requests
from bs4 import BeautifulSoup
url = "https://stackoverflow.com/questions/49168556/extract-css-selector-for-
an-element-with-selenium"
element = 'a'
idName = 'nav-questions'
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
tags = soup.find_all(element, id = idName)
if tags:
for tag in tags :
getClassNames = tag.get('class')
classNames = ''.join(str('.' + x) for x in getClassNames)
print element + '#' + idName + classNames
else:
print ':('
This would print something like: 这将打印如下内容:
a#nav-questions.-link.js-gps-track
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.