2017-09-22 7 views
0

こんにちは私はPythonを初めて使い、Webスクレイピングでショットを出しています。 これは私のコードは次のようになります。私は単に「リンク= browser.find_element」を取り出してCss_selectorで次のページに用語や手続を探してみましたので、したがって、上記のコードは私にエラーを与えているSelenium in Python:リンクが存在しない場合、次のページをクリックする方法を知らない

print('What is the term to be searched?') 

term = input() 

browser = webdriver.Firefox(executable_path ='/usr/local/bin/geckodriver') 
browser.get('https://google.com/search?q=' + term) 

try:  
    link = browser.find_element_by_link_text("Hello") 

# (I want to make it so the first page has no links of interest, so I can proceed to clicking the next page)  
except NoSuchElementException:  
    nextPage = browser.find_element_by_css_selector('Css of next page button)  
    nextPage.click() 

しかし、Tracebackエラーが出ました。

ご協力いただければ幸いです。おかげ

ここでは、コードのメッセージです:

Traceback (most recent call last): 
    File "/Users/shaun/Documents/Python/mwpractice.py", line 17, in <module> 
    nextPage = browser.find_element_by_css_selector('''<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>''') 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 498, in find_element_by_css_selector 
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element 
    'value': value})['value'] 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute 
    self.error_handler.check_response(response) 
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.InvalidSelectorException: Message: Given css selector expression "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>" is invalid: InvalidSelectorError: '<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>' is not a valid selector: "<span class="csb ch" style="background:url(/images/nav_logo242_hr.png) no-repeat;background-position:-74px 0;background-size:167px;width:20px"></span>" 
+0

完全なエラートレースバックを含めるように質問を編集します。 –

+0

「用語の検索」:どこですか?それはどんな言葉ですか? 「次のページボタンのCss」とは何ですか? Plsが指定します。また、エラーログも提供します。 –

+0

私はそれを更新しました –

答えて

0

あなたはより慎重にエラーメッセージを読まなければなりません。

selenium.common.exceptions.InvalidSelectorException:メッセージ:指定したCSSセレクタ式...」

それは非常に明確なメッセージです - あなたが提供するCSSセレクタが間違っている

。あなたは、次のページ]ボタンをクリックしたい場合は、XPathを使用することができます:次のページボタンの

browser.find_element_by_xpath('//*[@id="pnnext"]/span[2]').click() 

正しいCSSセレクタは次のとおりです。

#pnnext > span:nth-child(2) 
+0

私はこれを既に修正しましたが、応答に感謝します:)。私は直接のテキストリンクを行ったが、xpathがより具体的であるように感じる。 –

+0

ようこそ。 – mostaszewski

関連する問題