简体   繁体   English

如何使用 Selenium 和 Python 单击跨度元素

[英]How to click on the span element using Selenium and Python

I'm trying to identify the following element but no matter the method it doesn't see it.我正在尝试识别以下元素,但无论使用何种方法,它都看不到它。

<span onclick="onClickTab('details'); return false;" id = "details" name = "details" class style ="display: inline;"">...</span>

I've tried with: Xpath, relative xpath, onclick, onclick contains, by id, by name, just nothing works.我尝试过: Xpath, relative xpath, onclick, onclick contains, by id, by name,任何内容。 It is a clickable button which appears after selecting an item in a list.它是一个可点击的按钮,在选择列表中的项目后出现。

Current code is:当前代码是:

try:
WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[@onclick='onClickTab('details'); return false;']")))
except
print("Error")

driver.find_element_by_xpath("//span[@onclick='onClickTab('details'); return false;']").click()

if there are any minor syntax problems like a "(" or such it might be because I typed it by hand, that shouldn't be the issue. I'm forever grateful if you could point me to the right direction.如果有任何小的语法问题,例如“(”或类似的,可能是因为我是手动输入的,那不应该是问题。如果您能指出正确的方向,我将永远感激不尽。

I was able to select the element using the following xPath:我能够使用以下 xPath 对元素进行 select :

//span[contains(@onclick, "onClickTab(\'details\'); return false;")]

Using selenium, I used:使用 selenium,我使用:


try:
    WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH,'//span[@onclick="onClickTab(\'details\'); return false;"]')))
except:
    print("Error")

driver.find_element_by_xpath('//span[@onclick="onClickTab(\'details\'); return false;"]').click()

To click on the <span> element instead of presence_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies :要单击<span>元素而不是 present_of_element_located presence_of_element_located()您必须为element_to_be_clickable()诱导WebDriverWait并且您可以使用以下任一定位器策略

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#details[name='details'][onclick^='onClickTab']"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@id='details' and @name='details'][starts-with(@onclick, 'onClickTab')]"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

Here i suggest you to use id in Xpath.这里我建议你在 Xpath 中使用 id。

driver.find_element_by_xpath("//span[contains(@id,'details')]").click()

if multiple element is there then you have to use foreach loop of driver.find_elements_by_xpath("//span[contains(@id,'details')]") and check with text and click on match element.如果存在多个元素,那么您必须使用driver.find_elements_by_xpath("//span[contains(@id,'details')]") foreach 循环并检查文本并单击匹配元素。

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

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