selenium.common.exceptions.InvalidSelectorException 在 python 3.7 中使用 Selenium
selenium.common.exceptions.InvalidSelectorException using Selenium in python 3.7
我想使用 selenium 自动执行从打开特定网站到登录以搜索特定文章的过程。我可以执行的步骤很少,但在 'sign in' 步骤中遇到错误。
from selenium import webdriver
from selenium.webdriver.common.by import By
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()
browser.find_element_by_id('username').send_keys('**#&^&@$@#$')
browser.find_element_by_id('password').send_keys('@#@$%%**')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()
代码一直运行到这一行:
browser.find_element_by_id('na,us').click()
但在那之后它在这一行显示错误:
browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()
错误信息说:
selenium.common.exceptions.InvalidSelectorException: Message:
我的代码有什么问题?
这个错误信息...
selenium.common.exceptions.InvalidSelectorException
...暗示 XPath 表达式无效。
但是,您似乎很接近。您需要更换:
'//button[@type="button"],[contain(.,"Sign In")]'
并用and
运算符加入两个条件如下:
"//button[@type='button' and contains(.,'Sign In')]"
我想使用 selenium 自动执行从打开特定网站到登录以搜索特定文章的过程。我可以执行的步骤很少,但在 'sign in' 步骤中遇到错误。
from selenium import webdriver
from selenium.webdriver.common.by import By
base = 'https://www.wsj.com'
url = 'https://www.wsj.com/search/term.html?KEYWORDS=cybersecurity&min-date=2018/04/01&max-date=2019/03/31&isAdvanced=true&daysback=90d&andor=AND&sort=date-desc&source=wsjarticle,wsjpro&page=1'
browser = webdriver.Safari(executable_path='/usr/bin/safaridriver')
browser.get(url)
browser.find_element_by_id('editions-select').click()
browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()
browser.find_element_by_id('username').send_keys('**#&^&@$@#$')
browser.find_element_by_id('password').send_keys('@#@$%%**')
browser.find_element_by_id('basic-login').click()
browser.find_element_by_id('masthead-container').click()
browser.find_element_by_id('searchInput').send_keys('cybersecurity')
browser.find_element_by_name('ADVANCED SEARCH').click()
browser.find_element_by_id('dp1560924131783').send_keys('2018/04/01')
browser.find_element_by_id('dp1560924131784').send_keys('2019/03/31')
browser.find_element_by_id('wsjblogs').click()
browser.find_element_by_id('wsjvideo').click()
browser.find_element_by_id('interactivemedia').click()
browser.find_element_by_id('sitesearch').click()
代码一直运行到这一行:
browser.find_element_by_id('na,us').click()
但在那之后它在这一行显示错误:
browser.find_element(By.XPATH, '//button[@type="button"],[contain(.,"Sign In")]').click()
错误信息说:
selenium.common.exceptions.InvalidSelectorException: Message:
我的代码有什么问题?
这个错误信息...
selenium.common.exceptions.InvalidSelectorException
...暗示 XPath 表达式无效。
但是,您似乎很接近。您需要更换:
'//button[@type="button"],[contain(.,"Sign In")]'
并用and
运算符加入两个条件如下:
"//button[@type='button' and contains(.,'Sign In')]"