【问题标题】:Selenium webdriverwait to find element within elementSelenium webdriverwait 在元素中查找元素
【发布时间】:2017-12-03 19:57:07
【问题描述】:

我正在尝试单击动态更改位置的特定元素,因此它也会更改 xpathscss selectors

尝试过 xpath。

//*[@id="hld"]/div/div[X]/div[1]/h2/select

注意: X 的范围为 2 到 10,具体取决于各种因素。

没有class 名称或IDs 可供使用。我只需要处理tag 的名字。

我当前的代码如下。

h2 = driver.find_element_by_tag_name("h2")
select = h2.find_element_by_tag_name("select")
select.click()

不幸的是,选择标签将在 h2 标签之后加载一段时间,我正在尝试做一个 webdriverwait 等到元素可点击/可见,然后再运行上述代码。

遗憾的是,我并不清楚挑选出 select 元素的正确语法。下面是查找 h2 标记的代码,但我试图将其展开以专注于 select 标记。

WebDriverWait(driver, 30).until(
EC.visibility_of_element_located((By.TAG_NAME, "h2")))

非常感谢任何帮助。

【问题讨论】:

  • 与其解决问题,不如联系您的开发人员并要求他们为 Select Tag 提供唯一 ID。这将是最好和简单的解决方案。
  • @Tjj226_Angel 如果它是公共 URL,您能否考虑共享 URL?谢谢

标签: python selenium selenium-webdriver


【解决方案1】:

尝试删除动态 div 定位器 - 驱动程序将遍历页面上的元素并仅在存在时单击它。

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

xpath = ".//[@id="hld"]/div/div/div/h2/select"
timeout = 30    

WebDrierWait(driver, timeout).until(ec.presence_of_element_located(By.XPATH))

driver.find_element_by_xpath(xpath).click()

否则,如果驱动程序找到多个与您的 xpath 匹配的 xpath,您可以尝试以下操作:

elements = driver.find_elements_by_xpath(xpath)

for element in elements:
   try:
      element.click()
    except ElementNotVisibleException:
       pass
 
    

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2012-12-12
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2012-08-01
    相关资源
    最近更新 更多