読者です 読者をやめる 読者になる 読者になる

<Python, selenium> 空白のあるクラス名を選択するには、、

Seleniumで、空白のあるクラス名を選択するには、、、 how to select the class name having a blank in it?

まずは、xpathを使う。

In [133]: from selenium import webdriver

In [144]: d = webdriver.Chrome()

In [145]: d.get('http://nekoyukimmm.hatenablog.com/entry/2017/04/27/164336')

In [146]: d.find_element_by_xpath('//*[@id="box2-inner"]/div[3]')
Out[146]: <selenium.webdriver.remote.webelement.WebElement (session="a489f57900d9be9af6a6356176a89bd9", element="0.23540204403336795-1")>

In [148]: d.find_element_by_xpath('//*[@id="box2-inner"]/div[3]').text
Out[148]: 'カテゴリー\nselenium (2) Python (305) sentdex (2) pandas (86) Office2016 (1) numpy (7) pyplot (4) Javascript (15) jQuery (11) JSFiddle (3) flask (26) Vim (62) gspread (2) requests (2) Json (7) Google Cloud Platform (4) dos (5) conda (7) Github (11) msys2 (21) Anaconda (5) handsontable (1) Bootstrap (1) Google App Engine (5) Windows (24) matplotlib (27) seaborn (12) ATOM (5) datetime (2) SQLite (7) peewee (2) Apache (1) Beautiful Soup (7) Visual Studio (1) Linux (29) ftp (1) Werkzeug (1) html (11) tqdm (1) Unix Command (2) ssh (2) zsh (10) iPython (22) pacman (4) Cheatsheet (5) css (5) regexp (8) jinja (2) highcharts (8) socket (2) mermaid (2) Markdown (7) CDN (1) Bash (20) Network (2) Outlook (3) Pygments (1) Web (1) tmux (2) pip (5) Solarized (4) KDE (1) mintty (2) Putty (1) Go (2) peco (2) dotfiles (1) Jupyter (5) VBA (5) Excel (7) Surfingkeys (2) NeoBundle (2) SystemVerilog (1) x240 (4) MinGW+mintty (8) Sphinx (2) X (2) japandas (1) urllib (2) readability-lxml (2) html2text (1) Chrome (2) plotly (4) statistics (1) Office2013 (4) cron (1) bokeh (1) pptx (4) PIL (2) pdb (1) docopt (1) cgi (1) Jedi (4) XML (1) mpld3 (2) tput (1) iconv (1) mutt (1) Vundle (1) sed (1) Gow (2) ggplot (1) Perl (3)'

で、find_element_by_class_nameをしてみる。
ゲロ、、エラー。

In [155]: d.find_element_by_class_name('hatena-module hatena-module-category')
---------------------------------------------------------------------------
InvalidSelectorException                  Traceback (most recent call last)

その場合は、find_element_by_xpathで、div[@class='hage hage']とするらしい。

In [157]: d.find_element_by_xpath('//div[@class="hatena-module hatena-module-category"]')
Out[157]: <selenium.webdriver.remote.webelement.WebElement (session="a489f57900d9be9af6a6356176a89bd9", element="0.23540204403336795-1")>

In [158]: d.find_element_by_xpath('//div[@class="hatena-module hatena-module-category"]').text
Out[158]: 'カテゴリー\nselenium (2) Python (305) sentdex (2) pandas (86) Office2016 (1) numpy (7) pyplot (4) Javascript (15) jQuery (11) JSFiddle (3) flask (26) Vim (62) gspread (2) requests (2) Json (7) Google Cloud Platform (4) dos (5) conda (7) Github (11) msys2 (21) Anaconda (5) handsontable (1) Bootstrap (1) Google App Engine (5) Windows (24) matplotlib (27) seaborn (12) ATOM (5) datetime (2) SQLite (7) peewee (2) Apache (1) Beautiful Soup (7) Visual Studio (1) Linux (29) ftp (1) Werkzeug (1) html (11) tqdm (1) Unix Command (2) ssh (2) zsh (10) iPython (22) pacman (4) Cheatsheet (5) css (5) regexp (8) jinja (2) highcharts (8) socket (2) mermaid (2) Markdown (7) CDN (1) Bash (20) Network (2) Outlook (3) Pygments (1) Web (1) tmux (2) pip (5) Solarized (4) KDE (1) mintty (2) Putty (1) Go (2) peco (2) dotfiles (1) Jupyter (5) VBA (5) Excel (7) Surfingkeys (2) NeoBundle (2) SystemVerilog (1) x240 (4) MinGW+mintty (8) Sphinx (2) X (2) japandas (1) urllib (2) readability-lxml (2) html2text (1) Chrome (2) plotly (4) statistics (1) Office2013 (4) cron (1) bokeh (1) pptx (4) PIL (2) pdb (1) docopt (1) cgi (1) Jedi (4) XML (1) mpld3 (2) tput (1) iconv (1) mutt (1) Vundle (1) sed (1) Gow (2) ggplot (1) Perl (3)'

できた。

さんくー、スタックオーバーフロー。

stackoverflow.com