如何使用Python点击selenium在“https://maps.mapmyindia.com/direction”上点击“获取路线”?谢谢你的帮助!
我尝试了什么?我跟着这个“python selenium click on button”,但这不点击。
from selenium import webdriver
#from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox(executable_path=r'C:\Users\User\Desktop\pyCode\geckodriver-v0.21.0-win64\geckodriver.exe')
driver.get("https://maps.mapmyindia.com/direction")
startLocation = driver.find_element_by_id("auto_start")
startLocation.send_keys("28.4592,77.0727")
endLocation = driver.find_element_by_id("auto_end")
endLocation.send_keys("28.4590,77.0725")
driver.find_element_by_css_selector('div.col-xs-6.pull-right.text-right').click()
我查看了页面,似乎“获取路线”按钮有一个与之关联的ID。你可以简单地使用它
所以代码中的最后一行应该是:
driver.find_element_by_id("get_d").click()
您也可以使用其他选择器:
xpath: //a[text()='Get routes']
css: #get_d
编写测试脚本时,您始终可以在浏览器中验证选择器,然后再将它们包含在测试脚本中。以下是我遵循的一些简单方法来验证选择器:
document.getElementById("get_d")
。如果您使用的ID有效,则应在浏览器控制台中返回一个元素。$x("//a[text()='Get routes']")
。这也将返回与您提到的xpath相关联的所有元素$$("#get_d")
。与xpath方法类似,这将返回与您提到的css选择器关联的所有元素要在https://maps.mapmyindia.com/direction
上单击文本为GET ROUTES的元素,您需要:
style="display: none;"
click()
方法,文本为GET ROUTES。from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("https://maps.mapmyindia.com/direction")
search_tab = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.tab-pane.fade.in.search-tab.active")))
driver.execute_script("arguments[0].removeAttribute('style')", search_tab)
driver.find_element_by_css_selector("input.form-control.as-input#auto").send_keys("28.4592,77.0727")
driver.find_element_by_css_selector("input.form-control.as-input#auto_end").send_keys("28.4590,77.0725")
driver.find_element_by_css_selector("h2.get-btn>a.get-route#get_d").click()