在这个网站上做自动化测试时 http:/www.scstrade.comTechnicalAnalysistvchart 我无法用硒找到这个元素。
我想找到顶部的搜索栏元素,它是用来搜索股票的,然后使用硒在该栏中传递所需股票的名称。这里是搜索栏的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)
The element I am trying to access only has the class name so I can't try using the id.I also tried switching to the frame first but I can't even find the frame element for this website using selenium.
在左上角的搜索栏元素是在一个网站中。<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
你可以在中找到几个相关的讨论。