找到一个<;td>;元素使用seleniumpython

找到一个<;td>;元素使用seleniumpython,python,selenium,selenium-webdriver,xpath,xpath-1.0,Python,Selenium,Selenium Webdriver,Xpath,Xpath 1.0,我是python新手,我正在尝试编写一个web抓取脚本。我试图双击这个元素(它既不是按钮也不是链接——只是一个td元素),甚至在一开始就找不到它 下面是代码 业务概要文件(导入) 当我选择它时,类会改变。我怀疑这就是问题所在 业务概要文件(导入) 我使用了css选择器和xpath。都不管用。我尝试了两种方法: driver。通过xpath('//td[@title=“Business Profile(Imported)”)查找元素。单击() driver.find_element_by_cs

我是python新手,我正在尝试编写一个web抓取脚本。我试图双击这个元素(它既不是按钮也不是链接——只是一个td元素),甚至在一开始就找不到它

下面是代码

业务概要文件(导入)
当我选择它时,类会改变。我怀疑这就是问题所在

业务概要文件(导入)
我使用了css选择器和xpath。都不管用。我尝试了两种方法:

driver。通过xpath('//td[@title=“Business Profile(Imported)”)查找元素。单击()
driver.find_element_by_css_选择器(“td[title='businessprofile(Imported)')”)
这是我得到的错误:

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//td[@title=“业务概要文件(导入)”]”

任何帮助都将不胜感激。谢谢

要处理动态元素,请使用以下xpath

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//td[@title='Business Profile (Imported)' and text()='Business Profile (Imported)']"))).click()

您需要导入以下内容才能执行上述代码

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

要定位所需的
元素,您必须为位于()的元素的
可见性诱导WebDriverWait,并且您可以使用以下任一项:

  • XPATH
    1:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[contains(@title, 'Imported') and starts-with(., 'Business Profile')]")))
    
  • XPATH
    2:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[starts-with(@title, 'Business Profile') and contains(., 'Imported')]")))
    
  • 注意:您必须添加以下导入:

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

是否在IFRAME内?您是否尝试添加等待?您的定位器可能不需要同时检查标题和包含的文本。我想这两个答案中的任何一个都足够了。你的答案提供了什么而昆都克的答案没有?除了一个不那么具体和混乱的定位器???为什么要检查标题是否包含“已导入”,但包含的文本是否包含“业务概要”。。。为什么不检查标题或包含的文本?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC