selenium.common.exceptions.InvalidSelectorException在python 3.7中使用selenium
我想使用selenium自动化从打开特定网站到登录到搜索特定文章的过程。我可以做的步骤很少,但在“登录”步骤中遇到错误selenium.common.exceptions.InvalidSelectorException在python 3.7中使用selenium,selenium,selenium-webdriver,xpath,python-3.7,xpath-1.0,Selenium,Selenium Webdriver,Xpath,Python 3.7,Xpath 1.0,我想使用selenium自动化从打开特定网站到登录到搜索特定文章的过程。我可以做的步骤很少,但在“登录”步骤中遇到错误 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/0
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()
但在此之后,它会在这行中显示错误:
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")]'
并使用和运算符将这两个条件连接起来,如下所示:
"//button[@type='button' and contains(.,'Sign In')]"
不工作。同一行给出了一个错误,但这次错误与selenium.common.exceptions不同。NoSuchElementException@PiyushGhasiyaNoTouchElementException
是一个不同的错误,因为您当前的问题是InvalidSelectorException
。根据您的新要求,随时提出新问题。StackOverflow志愿者会很乐意帮助你。