繁体   English   中英

Selenium 在特定网站上找不到元素

[英]Selenium not finding elements on specific website

我正在尝试在https://www.ticketswap.be/上使用 Selenium,代码如下:

driver = webdriver.Chrome()
driver.get('https://www.ticketswap.be/')
login_button = driver.find_element(by=By.XPATH, value='//*[@id="__next"]/div[5]/div/nav/ul/li[4]/button')

不幸的是我收到以下错误:

---------------------------------------------------------------------------
NoSuchElementException                    Traceback (most recent call last)
<ipython-input-47-3a159bb5ece9> in <module>()
      1 driver = webdriver.Chrome(options = chrome_options)
      2 driver.get('https://www.ticketswap.be/')
----> 3 login_button = driver.find_element(by=By.XPATH, value='//*[@id="__next"]/div[5]/div/nav/ul/li[4]/button')

2 frames
/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    245                 alert_text = value['alert'].get('text')
    246             raise exception_class(message, screen, stacktrace, alert_text)  # type: ignore[call-arg]  # mypy is not smart enough here
--> 247         raise exception_class(message, screen, stacktrace)
    248 
    249     def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="__next"]/div[5]/div/nav/ul/li[4]/button"}
  (Session info: headless chrome=100.0.4896.127)
Stacktrace:
#0 0x562ead5a71b3 <unknown>
#1 0x562ead2962c3 <unknown>
#2 0x562ead2cc7a0 <unknown>
#3 0x562ead2cc9c1 <unknown>
#4 0x562ead301127 <unknown>
#5 0x562ead2ea11d <unknown>
#6 0x562ead2fee6c <unknown>
#7 0x562ead2ea463 <unknown>
#8 0x562ead2c063c <unknown>
#9 0x562ead2c1b05 <unknown>
#10 0x562ead5cba90 <unknown>
#11 0x562ead5dd378 <unknown>
#12 0x562ead5dd09c <unknown>
#13 0x562ead5dd902 <unknown>
#14 0x562ead615f0b <unknown>
#15 0x562ead5ddb61 <unknown>
#16 0x562ead5bffd3 <unknown>
#17 0x562ead5e72b8 <unknown>
#18 0x562ead5e744a <unknown>
#19 0x562ead600291 <unknown>

我曾尝试使用其他元素来查找登录按钮,但在该特定网站上均无效。 我还在其他网站上使用了相同的代码来定位那里的元素并且它有效。 任何人都可以帮助我完成这项工作吗?

编辑:我正在使用以下代码在 Google Colab 中实施 @cruisepandey 提出的解决方案:

!pip install selenium
!apt-get update # to update ubuntu to correctly run apt install
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(options = chrome_options)
driver.maximize_window()
wait = WebDriverWait(driver, 20)

driver.get("https://www.ticketswap.be/")
wait = WebDriverWait(driver, 20)

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accepteer']"))).click()

但我仍然收到以下错误:

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
<ipython-input-54-f6d2b0806de3> in <module>()
      6 wait = WebDriverWait(driver, 20)
      7 
----> 8 wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accepteer']"))).click()

/usr/local/lib/python3.7/dist-packages/selenium/webdriver/support/wait.py in until(self, method, message)
     87             if time.time() > end_time:
     88                 break
---> 89         raise TimeoutException(message, screen, stacktrace)
     90 
     91     def until_not(self, method, message=''):

TimeoutException: Message: 
Stacktrace:
#0 0x55a6dc2a91b3 <unknown>
#1 0x55a6dbf982c3 <unknown>
#2 0x55a6dbfce7a0 <unknown>
#3 0x55a6dbfce9c1 <unknown>
#4 0x55a6dc003127 <unknown>
#5 0x55a6dbfec11d <unknown>
#6 0x55a6dc000e6c <unknown>
#7 0x55a6dbfec463 <unknown>
#8 0x55a6dbfc263c <unknown>
#9 0x55a6dbfc3b05 <unknown>
#10 0x55a6dc2cda90 <unknown>
#11 0x55a6dc2df378 <unknown>
#12 0x55a6dc2df09c <unknown>
#13 0x55a6dc2df902 <unknown>
#14 0x55a6dc317f0b <unknown>
#15 0x55a6dc2dfb61 <unknown>
#16 0x55a6dc2c1fd3 <unknown>
#17 0x55a6dc2e92b8 <unknown>
#18 0x55a6dc2e944a <unknown>
#19 0x55a6dc302291 <unknown>
#20 0x7f9561dc26db <unknown>

您需要执行以下操作:

  1. 单击接受按钮。
  2. 单击关闭语言按钮。

代码:

driver = webdriver.Chrome()
driver.maximize_window()
wait = WebDriverWait(driver, 20)

driver.get("https://www.ticketswap.be/")
wait = WebDriverWait(driver, 20)

try:
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Accepteer']"))).click()
    print("Clicked on accept cookies button successfully")
    try:
        wait.until(EC.element_to_be_clickable((By.XPATH, "//*[name()='svg' and @aria-label='CloseRounded']"))).click()
        print("clicked on close language button")
    except:
        print("Could not click on close language button")
        pass
except:
    print("Could not click accept cookies button successfully")
    pass

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Inloggen']"))).click()

进口:

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

Output:

Clicked on accept cookies button successfully
clicked on close language button

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM