[英]How do I click a button with selenium
Hello Guys i'm tryng to click a button but is not working.大家好,我正在尝试单击按钮,但无法正常工作。 my code is like this:我的代码是这样的:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.implicitly_wait(0.5)
driver.maximize_window()
driver.get("https://www.freewayinsurance.com/")
driver.implicitly_wait(0.5)
element = driver.find_element(By.ID, "zipcode")
element.send_keys("60056")
element2 = driver.find_element(By.XPATH,"//div[@class='c-hero-home-radio__form-items']//button[@Class='c-button c-button--orange' and text()='Start Quote']")
element2.click()
I will apreciate if somebody can help me please thank you如果有人可以帮助我,我会很感激,谢谢
The Html looks like this: this is the HTML Html 看起来像这样:这是 HTML
The following code will input your zipcode, and click the button - the page will eventually load with insurance offers.以下代码将输入您的邮政编码,然后单击按钮 - 页面最终将加载保险报价。 The crux here is waiting for the element to load properly in page:这里的关键是等待元素在页面中正确加载:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
url = 'https://www.freewayinsurance.com/'
browser.get(url)
zipcode_field = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='zipcode']")))
zipcode_field.click()
zipcode_field.send_keys('90210')
print('Beverly Hills')
button = WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Start Quote']")))
button.click()
print('searching for insurance')
The selenium setup is for Linux, but you can adapt it to your own setup, just note the imports and the part after defining the browser/driver. selenium 设置适用于 Linux,但您可以根据自己的设置进行调整,只需注意定义浏览器/驱动程序后的导入和部分。 It will also print out in terminal:它还将在终端中打印出来:
Beverly Hills
searching for insurance
Selenium docs can be found at https://www.selenium.dev/documentation/ Selenium 文档可以在https://www.selenium.dev/documentation/找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.