Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
selenium.common.exceptions.NoSuchElementException在python 3.7中使用selenium_Selenium_Xpath_Python 3.7 - Fatal编程技术网

selenium.common.exceptions.NoSuchElementException在python 3.7中使用selenium

selenium.common.exceptions.NoSuchElementException在python 3.7中使用selenium,selenium,xpath,python-3.7,Selenium,Xpath,Python 3.7,我正在尝试使用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=201

我正在尝试使用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/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' and contains(.,'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' and contains(.,'Sign In')]").click()
但在此之后,它会在这行中显示错误:

browser.find_element_by_id('na,us').click()
browser.find_element(By.XPATH,"//button[@type='button' and contains(.,'Sign In')]").click()
错误消息显示:

selenium.common.exceptions.NoSuchElementException

我该怎么做才能让它工作?有什么问题吗?

您应该使用
WebDriverWait

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait

botton_to_click = WebDriverWait(driver, 10).until(EC.element_to_be_clickable, ((By.XPATH,"//button[@type='button' and contains(.,'Sign In')]")))
botton_to_click.click()

希望这对你有帮助

您这里的定位器没有出现在网站上…
contains(,'Sign-In')
非常完美,我觉得
contains(text(),'Sign-In')
没有优化。无法工作。显示行按钮单击时出错。单击()错误为:AttributeError:“元素可单击”对象没有属性“单击”否。错误:TypeError:“元组”对象没有属性callable@PiyushGhasiya对不起,我不在电脑上,所以很难纠正打字错误。。。一会儿我把它修好了,很抱歉给您带来不便…再次出错。在botton_to_click()行中显示错误错误为AttributeError:“element_to_be_clickable”对象没有属性“click”