Selenium 没有通过 id 找到元素 (python)
Selenium doesn't find element by id (python)
这是 html 元素:
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>
我的代码是:
option = webdriver.ChromeOptions()
option.add_argument("--mute-audio")
browser = webdriver.Chrome("C:/Users/leand/Desktop/kahoot-answer-bot-master/chromedriver.exe",options=option)
browser.get(url + "?launchApp=true")
browser.find_element_by_id('interstitial_join_btn').click()
但是我得到这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
...here is some info about the files etc...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="interstitial_join_btn"]"}
(Session info: chrome=89.0.4389.90)
我做错了什么或者有其他方法可以点击这个元素吗?
似乎同步issue.To点击使用WebDriverWait()
等待element_to_be_clickable()
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe"))) WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
导入以下库
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
如果上面的代码给你 timeout error
然后检查该按钮是否出现在 iframe
或 #shadowroot element
内
更新: 元素在iframe中需要切换。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser=webdriver.Chrome()
browser.get("https://meet37.webex.com/webappng/sites/meet37/meeting/download/7050764c36568ef62dd56dd671cb73c1?launchApp=true")
wait=WebDriverWait(browser,20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Your full name']"))).send_keys("user1")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Email address']"))).send_keys("Testuser@gmail.com")
wait.until(EC.element_to_be_clickable((By.ID,"guest_next-btn"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
要 select 必须首先从浏览器加载 selenium 上的项目,因此您需要等待几秒钟,直到所有 html 元素加载完毕,然后 select 它们
您可以像这样使用 time.sleep()
函数:
time.sleep(2.0) // 2.0 = 2seconds
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>
这是 html 元素:
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>
我的代码是:
option = webdriver.ChromeOptions()
option.add_argument("--mute-audio")
browser = webdriver.Chrome("C:/Users/leand/Desktop/kahoot-answer-bot-master/chromedriver.exe",options=option)
browser.get(url + "?launchApp=true")
browser.find_element_by_id('interstitial_join_btn').click()
但是我得到这个错误:
Exception in Tkinter callback
Traceback (most recent call last):
...here is some info about the files etc...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="interstitial_join_btn"]"}
(Session info: chrome=89.0.4389.90)
我做错了什么或者有其他方法可以点击这个元素吗?
似乎同步issue.To点击使用WebDriverWait()
等待element_to_be_clickable()
WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe"))) WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
导入以下库
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
如果上面的代码给你 timeout error
然后检查该按钮是否出现在 iframe
或 #shadowroot element
更新: 元素在iframe中需要切换。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser=webdriver.Chrome()
browser.get("https://meet37.webex.com/webappng/sites/meet37/meeting/download/7050764c36568ef62dd56dd671cb73c1?launchApp=true")
wait=WebDriverWait(browser,20)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID,"pbui_iframe")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Your full name']"))).send_keys("user1")
wait.until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Email address']"))).send_keys("Testuser@gmail.com")
wait.until(EC.element_to_be_clickable((By.ID,"guest_next-btn"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"interstitial_join_btn"))).click()
要 select 必须首先从浏览器加载 selenium 上的项目,因此您需要等待几秒钟,直到所有 html 元素加载完毕,然后 select 它们
您可以像这样使用 time.sleep()
函数:
time.sleep(2.0) // 2.0 = 2seconds
<button role="button" title="Meeting beitreten" id="interstitial_join_btn" class="style-rest-1IrDU style-theme-green-22KBC style-join-button-yqbh_ style-size-huge-3dFcq style-botton-outline-none-1M0ur" tabindex="1" aria-label="Meeting beitreten" aria-describedby="current_select_status" data-doi="MEETING:JOIN_MEETING:MEETSIMPLE_INTERSTITIAL">Meeting beitreten</button>