如何使用Selenium和Python按照html单击按钮
我正在学习Selenium并尝试单击如何使用Selenium和Python按照html单击按钮,python,selenium,selenium-webdriver,webdriver,webdriverwait,Python,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,我正在学习Selenium并尝试单击GO按钮: 去 点击按钮的所有可能的方法是什么, elem=驱动程序。通过查找元素来查找元素 我还想看看我发现了什么,那么应该使用print(elem.text)吗?你必须使用xpath,chrome有xpath帮助工具。你可以安装它 button = driver.find_element_by_xpath("your xpath") button.click() 试试这个: browser.find_element_by_class_name("
GO
按钮:
去
点击按钮的所有可能的方法是什么,
elem=驱动程序。通过查找元素来查找元素
我还想看看我发现了什么,那么应该使用print(elem.text)
吗?你必须使用xpath
,chrome有xpath
帮助工具。你可以安装它
button = driver.find_element_by_xpath("your xpath")
button.click()
试试这个:
browser.find_element_by_class_name("button background-primary-hover text-primary").click()
因为它将选择元素并单击它。根据网站https://speedtest.telstra.com/
所需元素位于
中,因此您需要引导WebDriverWait切换到
,然后查找该元素,您可以使用以下解决方案:
- 使用
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\u选择器
:
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单击的选项。我已经为这个解决方案研究了2个小时。我希望我能找到它,但谢谢你让我高兴起来。我不知道WebDriverWait
。我从这个答案中学到了很多。谢谢,谢谢,所以通过XPATH定位可点击的。使用类名或CSS选择器我该如何做?@user3013157签出我的更新答案并让我知道状态。@New contributor,噢,非常感谢,我现在明白了:)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC