如何使用 selenium 和 Python 识别此元素?
How to identify this element using selenium and Python?
我已经尝试 select 使用 xpath、id、class 下面的元素,但没有任何效果。这是错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ui-id-26"]"}
HTML 元素代码:
<a title="" href="#8eef8ef4-7cfc-4cb6-69cf-1cabf9625d74" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-26">Data</a>
元素的快照:
我正在使用 python。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
chromedriver = 'C:/Users/misha/Downloads/chromedriver_win32/chromedriver'
driver = webdriver.Chrome(chromedriver)
driver.get("https://login.cmegroup.com/sso/accountstatus/showAuth.action")
driver.maximize_window()
login = driver.find_element(By.ID, "loginBtn")
name = driver.find_element(By.ID, "user")
name.send_keys("MyEmail")
pwd = driver.find_element(By.ID, "pwd")
pwd.send_keys("MyPassword")
login.click()
time.sleep(15)
driver.get('https://www.cmegroup.com/trading/fx/cme-fx-market-profile-tool.html#analyze-more')
data = driver.find_element(By.XPATH,'//*[@id="ui-id-26"]')
data.click()
要单击文本为 Data 的元素,您可以使用以下任一方法 :
使用link_text:
driver.find_element(By.LINK_TEXT, "Data").click()
使用css_selector:
driver.find_element(By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]").click()
使用 xpath:
driver.find_element(By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']").click()
想要的元素是动态元素,所以最好 on the clickable element you need to induce WebDriverWait for the and you can use either of the following :
使用LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Data"))).click()
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]"))).click()
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于 的一些相关讨论:
我已经尝试 select 使用 xpath、id、class 下面的元素,但没有任何效果。这是错误消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ui-id-26"]"}
HTML 元素代码:
<a title="" href="#8eef8ef4-7cfc-4cb6-69cf-1cabf9625d74" class="ui-tabs-anchor" role="presentation" tabindex="-1" id="ui-id-26">Data</a>
元素的快照:
我正在使用 python。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
chromedriver = 'C:/Users/misha/Downloads/chromedriver_win32/chromedriver'
driver = webdriver.Chrome(chromedriver)
driver.get("https://login.cmegroup.com/sso/accountstatus/showAuth.action")
driver.maximize_window()
login = driver.find_element(By.ID, "loginBtn")
name = driver.find_element(By.ID, "user")
name.send_keys("MyEmail")
pwd = driver.find_element(By.ID, "pwd")
pwd.send_keys("MyPassword")
login.click()
time.sleep(15)
driver.get('https://www.cmegroup.com/trading/fx/cme-fx-market-profile-tool.html#analyze-more')
data = driver.find_element(By.XPATH,'//*[@id="ui-id-26"]')
data.click()
要单击文本为 Data 的元素,您可以使用以下任一方法
使用link_text:
driver.find_element(By.LINK_TEXT, "Data").click()
使用css_selector:
driver.find_element(By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]").click()
使用 xpath:
driver.find_element(By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']").click()
想要的元素是动态元素,所以最好
使用LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Data"))).click()
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ui-tabs-anchor[id^='ui-id'][title]"))).click()
使用 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='ui-tabs-anchor' and starts-with(@id, 'ui-id')][text()='Data']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
参考资料
您可以在以下位置找到关于