如何使用 Selenium 和 Python 在 url https://maps.mapmyindia.com/direction 中单击文本为 "GET ROUTES" 的元素?
How to click on element with text as "GET ROUTES" within the url https://maps.mapmyindia.com/direction using Selenium and Python?
如何使用 Python 通过 selenium 在“https://maps.mapmyindia.com/direction”上点击 "get routes"?感谢您的帮助!
我尝试了什么?
我关注了这个“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()
我查看了页面,"get routes" 按钮似乎有一个关联的 ID。你可以简单地使用那个
所以你代码的最后一行应该是:
driver.find_element_by_id("get_d").click()
您也可以使用其他选择器:
xpath: //a[text()='Get routes']
css: #get_d
编写测试脚本时,您始终可以在浏览器中验证选择器,然后再将其包含在测试脚本中。下面是我用来验证选择器的一些简单方法:
- 使用 'id' 时,只需在浏览器控制台中使用以下 javascript:
document.getElementById("get_d")
。如果您使用的 ID 有效,这应该 return 浏览器控制台中的一个元素。
- 使用 'xpath' 时,请在浏览器控制台中使用以下行:
$x("//a[text()='Get routes']")
。这也将 return 与您提到的 xpath 关联的所有元素
- 使用 'css selector' 时,请使用以下行:
$$("#get_d")
。类似于 xpath 方法,这将 return 与您提到的 css 选择器关联的所有元素
要在 https://maps.mapmyindia.com/direction
上单击文本为 GET ROUTES 的元素,您需要:
- 诱导 WebDriverWait 使 元素容器出现 在 HTML DOM.
中
- 然后需要去掉属性
style="display: none;"
- 最后,您可以发送字符序列并在文本为 GET ROUTES.
的元素上调用 click()
方法
代码块:
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()
浏览器快照:
如何使用 Python 通过 selenium 在“https://maps.mapmyindia.com/direction”上点击 "get routes"?感谢您的帮助!
我尝试了什么? 我关注了这个“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()
我查看了页面,"get routes" 按钮似乎有一个关联的 ID。你可以简单地使用那个
所以你代码的最后一行应该是:
driver.find_element_by_id("get_d").click()
您也可以使用其他选择器:
xpath: //a[text()='Get routes']
css: #get_d
编写测试脚本时,您始终可以在浏览器中验证选择器,然后再将其包含在测试脚本中。下面是我用来验证选择器的一些简单方法:
- 使用 'id' 时,只需在浏览器控制台中使用以下 javascript:
document.getElementById("get_d")
。如果您使用的 ID 有效,这应该 return 浏览器控制台中的一个元素。 - 使用 'xpath' 时,请在浏览器控制台中使用以下行:
$x("//a[text()='Get routes']")
。这也将 return 与您提到的 xpath 关联的所有元素 - 使用 'css selector' 时,请使用以下行:
$$("#get_d")
。类似于 xpath 方法,这将 return 与您提到的 css 选择器关联的所有元素
要在 https://maps.mapmyindia.com/direction
上单击文本为 GET ROUTES 的元素,您需要:
- 诱导 WebDriverWait 使 元素容器出现 在 HTML DOM. 中
- 然后需要去掉属性
style="display: none;"
- 最后,您可以发送字符序列并在文本为 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()
浏览器快照:
click()
方法