代码之家  ›  专栏  ›  技术社区  ›  iamabhaykmr

在下拉结果python selenium库中的任意位置单击下拉搜索结果项

  •  0
  • iamabhaykmr  · 技术社区  · 1 年前

    我一直在试着在下面的屏幕截图中选择搜索到的元素。我尝试了以下方法,但似乎都找不到下拉搜索结果元素。

    enter image description here 在检查结果的基础上,共享下面下拉结果的相应源代码。

    enter image description here

    到目前为止,我尝试了以下方法 selenium python库。

        span_class="Roboto--ezzN4 text-5--NzQT7"
        tag_name = "aria-label"
        drop_down_tag_result = driver.find_element(by="class name", value=span_class)
        drop_down_tag_result = driver.find_element(by="tag name", value=tag_name)    
        
        drop_down_result = driver.find_element(by="xpath", value="//div[text()="+"'"+brand_name+"'"+"]")
        drop_down_result.click()
    

    以上三种方法都无法定位元素,下面会出现类似的错误。

    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".Roboto--ezzN4 text-5--NzQT7"}
      (Session info: chrome=120.0.6099.109); 
    

    有人能帮我指引正确的方向吗?我是硒的新手。

    0 回复  |  直到 1 年前
        1
  •  1
  •   KunduK    1 年前

    可能存在同步问题。在将元素与预期条件交互之前使用显式等待请参阅此处的示例--> Waits

    请更改 xpath 以唯一地识别该元素。

    drop_down_result = driver.find_element(by="xpath", value="//span[text()='" + brand_name + "']")
    

    drop_down_result = driver.find_element(by="xpath", value="//div[@aria-label='" + brand_name + "']")