当前位置: 首页 > 面试题库 >

无法找到元素Selenium WebDriver

米迪
2023-03-14
问题内容

我正在尝试抓取以下网站:https :
//www.bancosantander.es/es/particulares/prestamos/prestamo-
coche/simulador

我正在尝试做的是:

通过移动滑块来模拟数量和持续时间。我正在尝试使用网络驱动程序找到它们。

但是我不能,事实上我已经开始抓取了。

出现的错误如下:

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:12: DeprecationWarning: use options instead of chrome_options
  if sys.path[0] == '':

---------------------------------------------------------------------------

NoSuchElementException                    Traceback (most recent call last)

<ipython-input-29-f71d54453a68> in <module>()
     20     pass
     21 webdriver.save_screenshot('sreenshot0.png')
---> 22 webdriver.find_element_by_xpath("//*[@class='res r1']")
     23 
     24 webdriver.save_screenshot('sreenshot0.png')

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in find_element_by_xpath(self, xpath)
    392             element = driver.find_element_by_xpath('//div/td[1]')
    393         """
--> 394         return self.find_element(by=By.XPATH, value=xpath)
    395 
    396     def find_elements_by_xpath(self, xpath):

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in find_element(self, by, value)
    976         return self.execute(Command.FIND_ELEMENT, {
    977             'using': by,
--> 978             'value': value})['value']
    979 
    980     def find_elements(self, by=By.ID, value=None):

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='res r1']"}
  (Session info: headless chrome=72.0.3626.121)
  (Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.79+ x86_64)

我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-maximized')
chrome_options.add_argument('window-size=4400x2200')
webdriver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
webdriver.maximize_window()
url = "https://www.bancosantander.es/es/particulares/prestamos/prestamo-coche/simulador"
webdriver.get(url)

try:
    webdriver.find_element_by_xpath("//button[@onclick=\"_W034_Cookie_Directive_WAR_W034_Cookie_Directiveportlet_.hidePortlet()\"]").click()
except NoSuchElementException:
    pass
webdriver.save_screenshot('sreenshot0.png')
webdriver.find_element_by_xpath("//*[@class='res r1']")

webdriver.save_screenshot('sreenshot0.png')

问题答案:

您可以尝试以下方法输入值

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys

url = 'https://simuladores.bancosantander.es/SantanderES/loansimulatorweb.aspx?por=webpublica&prv=publico&m=100&cta=1&ls=0#/t0'
driver = webdriver.Chrome()
driver.get(url)

amount =  WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#amount")))
amount.clear()
amount.send_keys(6000)

WebDriverWait(driver, 30).until(lambda driver: driver.execute_script('return jQuery.active') == 0)

term =  WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "#term")))
term.clear()
term.send_keys('60 meses')
term.send_keys(Keys.TAB)
currentCuota = driver.find_element_by_css_selector('.r1 span').text
WebDriverWait(driver, 10).until(lambda driver: driver.execute_script('return jQuery.active') == 0)
while driver.find_element_by_css_selector('.r1 span').text == currentCuota:
    continue

print('Tu cuota mensual: ' + driver.find_element_by_css_selector('.r1 span').text)


 类似资料:
  • 问题内容: 我正在尝试使用selenium在kahoot.it网页上自动生成大量用户,以使它们出现在类的前面,但是,当我尝试访问inputSession项时(在其中写入gameID进入游戏) 这是错误: 任何帮助将非常感激!:) 问题答案: 可能是竞标条件,在该竞标条件下find元素在页面上出现之前就已执行。看一下等待超时文档。这是文档中的示例

  • [[Img1][Img2[Img3]我试图通过链接文本获取元素。但是低于例外。异常在线程"main"org.openqa.selenium.NoSuchElement异常:没有这样的元素:无法定位元素:{"方法":"链接文本","选择器":"CFDSDSR"} 我尝试切换到帧,但它给出了未找到的帧,但该帧存在: XPath: /html/body/table/tbody/tr/td/form/ta

  • 问题内容: 我有一个工作脚本,可以使用selenium登录到站点,如下所示: script.py 通过以下方式在安装了Firefox的 亚马逊Ubuntu盒子 上运行该脚本: 我得到的错误是: selenium.common.exceptions.NoSuchElementException:消息:u’无法找到元素:{“ method”:“ id”,“ selector”:“ content”}’

  • 我试图通过xpath获取一个输入元素,但是return语句是false。 当我试图获取标签(它是输入的唯一兄弟)时,它返回true。我也尝试通过输入类型[@type=“checkbox”]获取它,但也失败了。 null

  • 元素的HTML为: 来自Firebug的X路径: