python Selenium无法点击按钮

5rgfhyps  于 2023-02-11  发布在  Python
关注(0)|答案(3)|浏览(341)

https://mokivezi.lt/leidiniai中有一个名为“打开”的按钮,您可以在目录图像下看到它。
它的要素是:<button data-href="open" aria-label="Open UAB &quot;Makveža&quot; - Pagrindinis Moki-vezi kaininis leidinys">Open</button>
使用Python SeleniumEdge/Chromewebdrivers时,我无法单击该按钮。我尝试切换到不同的iframe,但仍会出现附加错误。

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_14248\1636534134.py in <module>
----> 1 WebDriverWait(driver, 5).until(
      2     EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))
      3 ).click()

~\Anaconda3\envs\webscraping\lib\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     93             if time.monotonic() > end_time:
     94                 break
---> 95         raise TimeoutException(message, screen, stacktrace)
     96 
     97     def until_not(self, method, message: str = ""):

TimeoutException: Message: 
Stacktrace:
Backtrace:
    Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF7262B16C2+15186]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72624A212+827554]
    (No symbol) [0x00007FF725F0ED90]
    (No symbol) [0x00007FF725F52225]
    (No symbol) [0x00007FF725F523AC]
    (No symbol) [0x00007FF725F8E087]
    (No symbol) [0x00007FF725F71F8F]
    (No symbol) [0x00007FF725F44C3E]
    (No symbol) [0x00007FF725F8B513]
    (No symbol) [0x00007FF725F71D23]
    (No symbol) [0x00007FF725F43B80]
    (No symbol) [0x00007FF725F42B0E]
    (No symbol) [0x00007FF725F44344]
    Microsoft::Applications::Events::EventProperties::SetProperty [0x00007FF72612C3B0+182752]
    (No symbol) [0x00007FF726000095]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618A6EA+42362]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72618D425+53941]
    Microsoft::Applications::Events::ILogManager::DispatchEventBroadcast [0x00007FF7264A8AB3+1456595]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72625276A+861690]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF726257854+882404]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF7262579AC+882748]
    Microsoft::Applications::Events::EventProperty::EventProperty [0x00007FF72626097E+919566]
    BaseThreadInitThunk [0x00007FFF40C77AD4+20]
    RtlUserThreadStart [0x00007FFF436CA371+33]

我做错了什么?要单击,我尝试使用WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))).click()

ewm0tg9j

ewm0tg9j1#

Open按钮位于iframe中。您需要先切换到iframe,才能访问元素。
使用WebDriverWait()并等待frame_to_be_available_and_switch_to_it()

driver.get("https://mokivezi.lt/leidiniai")
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src='//view.publitas.com/uab-makveza/pagrindinis-moki-vezi_kedainiu_parduotuve/?publitas_embed=embedded']")))
openButton=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Open']")))
driver.execute_script("arguments[0].click();", openButton)

**浏览器快照:**单击“打开”按钮后

kr98yfug

kr98yfug2#

问题是您尝试单击的元素不存在
显示“Open”的元素如下

<button data-href="open" aria-label="Open UAB &quot;Makveža&quot; - Pagrindinis Moki-vezi kaininis leidinys">
    Open
</button>

不妨试试这个

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Open']"))).click()
vltsax25

vltsax253#

你可以用pyautogui来做。只要修改坐标。

import pyautogui
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(r'C:\Users\eljabbarli\Downloads\chromedriver_win3\chromedriver.exe')

driver.get("https://mokivezi.lt/leidiniai")
time.sleep(3)
search_button = driver.find_element(By.XPATH,'//*[@id="cookie-holder"]/div/button')
search_button.click()
pyautogui.moveTo(670, 750)
pyautogui.click()
driver.quit()

相关问题