我正在学习Selenium并试图点击GO
按钮:https://speedtest.telstra.com/
<button class="button background-primary-hover text-primary" aria-label="start your speedtest">
<span style="font-size: unset; overflow-wrap: unset;">GO</span></button>
什么是可能的Selenium方法来点击该按钮,elem = driver.find_element_by_
.... ???
我也想看看我发现了什么,那么print(elem.text)
应该被使用吗?
根据网站https://speedtest.telstra.com/
所需的元素在<iframe>
中,所以你需要诱导WebDriverWait切换到<iframe>
,然后查找元素,你可以使用以下解决方案:
XPATH
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='speed-test' and @src='//telstra-nbn.speedtestcustom.com']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='button background-primary-hover text-primary']/span[contains(.,'GO')]"))).click()
CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"//iframe.speed-test[src*='speedtestcustom']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button.background-primary-hover.text-primary[aria-label='start your speedtest']>span"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
你必须使用xpath
,有xpath
辅助工具铬。你可以安装它。
button = driver.find_element_by_xpath("your xpath")
button.click()
试试这个:
browser.find_element_by_class_name("button background-primary-hover text-primary").click()
因为它会选择元素并单击它。