如何在 python 中使用 selenium select 以下元素
How to select the following element using selenium in python
我正在尝试 select 的元素是下面的那个
<div id="mOnlineEligiblity" class="col span_1_of_3" style="">
<a href="onlineElig.html" style="text-decoration: none !important;color: #275883;"><img src="../../images/mobileHome/Newicon/OnlineEligibility.png" height="65px" width="65px" class="morph">
<br> Online Eligibility </a>
</div>
我尝试了以下 select 它,但它说找不到元素
driver.find_element_by_id("mOnlineEligiblity")
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="mOnlineEligiblity"]"}
我希望能够select并点击它
编辑:-
它存在于 iframe
中,如 Swaroop 所述,附上图像以供参考
不得不像 Swaroop 提到的那样切换到 iframe
。
# Find the iframe
iframe = driver.find_element_by_xpath("//iframe[@name='NAME_OF_THE_FRAME']")
# Switch to the iframe
driver.switch_to.frame(iframe)
# Now find the element
driver.find_element_by_id("mOnlineEligiblity").click()
只要它确实在您正在加载的页面上(不在 iframe 中),那么定位和点击它应该没有任何问题。
import platform
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
from selenium.webdriver.firefox.options import Options
options = webdriver.ChromeOptions()
if platform == "linux" or platform == "linux2":
driver = webdriver.Chrome(executable_path='chromedriver_linux', chrome_options=options)
elif platform == "darwin":
driver = webdriver.Chrome(executable_path='chromedriver_mac', chrome_options=options)
else:
driver = webdriver.Firefox()
driver.maximize_window()
timeout = 10
driver.get("file:///G:/Downloads/random.html")
xpath = '//*[@id="mOnlineEligiblity"]/a'
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.XPATH, xpath)))
link_to_click = driver.find_element(By.XPATH, XPath)
link_to_click.click()
driver.quit()
我仍然建议通过 xpath = '//*[@id="mOnlineEligiblity"]/a'
而不是仅 xpath = '//*[@id="mOnlineEligiblity"]'
定位您实际想要点击的元素,但如果它是one-off 脚本。
我正在尝试 select 的元素是下面的那个
<div id="mOnlineEligiblity" class="col span_1_of_3" style="">
<a href="onlineElig.html" style="text-decoration: none !important;color: #275883;"><img src="../../images/mobileHome/Newicon/OnlineEligibility.png" height="65px" width="65px" class="morph">
<br> Online Eligibility </a>
</div>
我尝试了以下 select 它,但它说找不到元素
driver.find_element_by_id("mOnlineEligiblity")
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="mOnlineEligiblity"]"}
我希望能够select并点击它
编辑:-
它存在于 iframe
中,如 Swaroop 所述,附上图像以供参考
不得不像 Swaroop 提到的那样切换到 iframe
。
# Find the iframe
iframe = driver.find_element_by_xpath("//iframe[@name='NAME_OF_THE_FRAME']")
# Switch to the iframe
driver.switch_to.frame(iframe)
# Now find the element
driver.find_element_by_id("mOnlineEligiblity").click()
只要它确实在您正在加载的页面上(不在 iframe 中),那么定位和点击它应该没有任何问题。
import platform
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
from selenium.webdriver.firefox.options import Options
options = webdriver.ChromeOptions()
if platform == "linux" or platform == "linux2":
driver = webdriver.Chrome(executable_path='chromedriver_linux', chrome_options=options)
elif platform == "darwin":
driver = webdriver.Chrome(executable_path='chromedriver_mac', chrome_options=options)
else:
driver = webdriver.Firefox()
driver.maximize_window()
timeout = 10
driver.get("file:///G:/Downloads/random.html")
xpath = '//*[@id="mOnlineEligiblity"]/a'
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.XPATH, xpath)))
link_to_click = driver.find_element(By.XPATH, XPath)
link_to_click.click()
driver.quit()
我仍然建议通过 xpath = '//*[@id="mOnlineEligiblity"]/a'
而不是仅 xpath = '//*[@id="mOnlineEligiblity"]'
定位您实际想要点击的元素,但如果它是one-off 脚本。