selenium 按类名查找元素时会出现以下错误:“追踪(最近的呼叫排在最后)”

7cwmlq89  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(113)

我登录到Instagram,我试图按下一个按钮关闭弹出窗口,但每次得到这个无论我怎么尝试,* 我repleced用户名和密码只把代码在这里。

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time

path = "/Users/almogbenlulu/Desktop/Almog/python/chromedriver.exe"
s = Service(path)
driver = webdriver.Chrome(service=s)

driver.get("https://www.instagram.com/")

WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, 'username')))

driver.find_element(By.NAME, "username").send_keys("*****")
driver.find_element(By.NAME, "password").send_keys("*****")
driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button/div').click()

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_acan _acao _acas')))

driver.find_element(By.CLASS_NAME, '_acan _acao _acas').click()

driver.quit()

我总是得到这个即使没有等待元素

Traceback (most recent call last):
  File "/Users/almogbenlulu/Desktop/Almog/python/Automation/venv/day1/insta.py", line 22, in <module>
    WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_acan _acao _acas')))
  File "/Users/almogbenlulu/Desktop/Almog/python/Automation/venv/lib/python3.11/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
0   chromedriver                        0x00000001048052c8 chromedriver + 4752072
1   chromedriver                        0x0000000104785463 chromedriver + 4228195
2   chromedriver                        0x00000001043e8b18 chromedriver + 441112
3   chromedriver                        0x0000000104425e21 chromedriver + 691745
4   chromedriver                        0x0000000104426061 chromedriver + 692321
5   chromedriver                        0x00000001044615e4 chromedriver + 935396
6   chromedriver                        0x0000000104446d2d chromedriver + 826669
7   chromedriver                        0x000000010445f134 chromedriver + 926004
8   chromedriver                        0x0000000104446b33 chromedriver + 826163
9   chromedriver                        0x00000001044179fd chromedriver + 633341
10  chromedriver                        0x0000000104419051 chromedriver + 639057
11  chromedriver                        0x00000001047d230e chromedriver + 4543246
12  chromedriver                        0x00000001047d6a88 chromedriver + 4561544
13  chromedriver                        0x00000001047de6df chromedriver + 4593375
14  chromedriver                        0x00000001047d78fa chromedriver + 4565242
15  chromedriver                        0x00000001047ad2cf chromedriver + 4391631
16  chromedriver                        0x00000001047f65b8 chromedriver + 4691384
17  chromedriver                        0x00000001047f6739 chromedriver + 4691769
18  chromedriver                        0x000000010480c81e chromedriver + 4782110
19  libsystem_pthread.dylib             0x00007ff8188bd4e1 _pthread_start + 125
20  libsystem_pthread.dylib             0x00007ff8188b8f6b thread_start + 15

Process finished with exit code 1

这就是我想抓住的

<div class="_ac8f"><button class="_acan _acao _acas" type="button">Not Now</button></div>
e0bqpujr

e0bqpujr1#

By.CLASS_NAME接收单个参数值,而_acan _acao _acas是3个类名。
若要根据多个类名查找元素,可以使用CSS选择器或XPath。
所以,与其

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, '_acan _acao _acas')))

尝试使用

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '._acan._acao._acas')))

同样,上面的行返回web元素。因此,要单击它,您不需要使用driver.find_element(By.CLASS_NAME, '_acan _acao _acas').click()再次定位它。

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '._acan._acao._acas'))).click()

相关问题