简体   繁体   English

无法通过 python 找到跨度 class

[英]Unable to locate a span class by python

I have a span class attribute which I need to click and pass value to it.我有一个跨度 class 属性,我需要单击它并将值传递给它。

Below is my span class:下面是我的跨度 class:

<span class="input-group-addon-transparent icon-search sysparm-search-icon"></span>

Please do help me out.请帮帮我。 Thanks in Advance.提前致谢。

Without knowing the full HTML of the site you can get the first span which matches those classes by using find_element_by_css_selector :在不知道站点的完整 HTML 的情况下,您可以使用find_element_by_css_selector获得与这些类匹配的第一个跨度:

selector = "span.input-group-addon-transparent.icon-search.sysparm-search-icon"
element = driver.find_element_by_css_selector(selector)
element.click()
element.sendKeys("value")

Or waiting for clickable state:或者等待可点击的 state:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

selector = "span.input-group-addon-transparent.icon-search.sysparm-search-icon"
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, selector)))
element.sendKeys("value")

Where driver is your Selenium webdriver object driver是您的 Selenium webdriver object

You case use class name to get that webelement.您的案例使用 class 名称来获取该 web 元素。

 webele=  driver.find_element_by_class_name("input-group-addon-transparent icon-search sysparm-search-icon")
webele.click();
webele.sendkeys("any string/number")

or或者

driver.find_element_by_xpath("//*[contains(@class,'search-icon')]

//and then rest of the code //然后是rest的代码

Use web driver wait before clicking on button使用 web 驱动等待点击按钮

WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, "XPATH")) 

Find the xpath of the span using any browser console then try using this code below to click and pass the value to it使用任何浏览器控制台找到跨度的 xpath 然后尝试使用下面的代码单击并将值传递给它

span = find_element_by_xpath('''the x_path of the class''').click()
span.sendKeys('''value to be passed''')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM