在此网站http://www.scstrade.com/TechnicalAnalysis/tvchart/上进行自动化测试时,我无法使用硒找到元素。
我想在顶部找到用于搜索股票的搜索栏元素,然后使用硒在该栏中传递所需股票的名称。
这是搜索栏的xpath:
/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input
这是我的代码:
from selenium import webdriver
driver = webdriver.Chrome("D:\PyCharm Projects\Web Automation\drivers\chromedriver.exe")
driver.get("http://www.scstrade.com/TechnicalAnalysis/tvchart/")
driver.find_elements_by_xpath('/html/body/div[1]/div[2]/div/div/div[1]/div/div/div/div/div[1]/div/div/input')
Output : []
我还尝试使用类名称查找元素:
driver.find_element_by_class_name('input-3lfOzLDc-')
这给了我这个错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"class name","selector":"input-3lfOzLDc-"}
(Session info: chrome=83.0.4103.61)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 10.0.17134 x86_64)
我尝试访问的元素仅具有类名,因此我无法尝试使用ID。我也尝试过先切换到框架,但我什至找不到使用硒的网站的框架元素。
左上角的搜索栏元素位于内,<iframe>
因此send_keys()
您必须调用该元素:
frame_to_be_available_and_switch_to_it()
。element_to_be_clickable()
。您可以使用以下定位策略之一:
使用CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='charting_library/static/en-tv-chart']")))
index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#header-toolbar-symbol-search input")))
index.click()
index.clear()
index.send_keys("KSE 30")
使用XPATH
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'charting_library/static/en-tv-chart')]")))
index = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='header-toolbar-symbol-search']//input")))
index.click()
index.clear()
index.send_keys("KSE 30")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
您可以在以下位置找到一些相关的讨论:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句