こんにちは私は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>"
完全なエラートレースバックを含めるように質問を編集します。 –
「用語の検索」:どこですか?それはどんな言葉ですか? 「次のページボタンのCss」とは何ですか? Plsが指定します。また、エラーログも提供します。 –
私はそれを更新しました –