【问题标题】:Click on dropdown list doesn't perform单击下拉列表不执行
【发布时间】:2022-01-02 13:41:06
【问题描述】:

enter image description here我是使用 selenium 和练习的 Python 新手。我正在尝试单击作为下拉列表的此元素,但该元素已突出显示但未单击。您可以在下面找到我尝试过的代码以及我正在检查的屏幕截图和 HTML。

   driver.implicitly_wait(30)[enter image description here][1]
   time.sleep(10)
   print("The Non fiction is selected")
   # choose_category_in_list = driver.find_element(By.ID, "div-nonfiction").click()
   # choose_category_in_list = driver.find_element(By.ID, "div-nonfiction").click()
   # button = driver.find_element_by_link_text("Nonfiction")
   # choose_category_in_list = driver.find_element(By.XPATH , "//a[contains(text(),'Nonfiction')]").click()
   element = driver.find_element_by_xpath("//a[contains(text(),'Nonfiction')]")
   driver.execute_script("arguments[0].click();", element)
   # action = TouchActions(driver)
   # action.tap(mySelect).perform()
   # mySelect.select_by_visible_text('ATLANTIC')
   # choose_category_in_list = driver.find_element(By.ID, "icon-nonfiction").click()
   # # choose_category_in_list = driver.find_element(By.ID, "icon-nonfiction").click()
   # # choose_category_in_list = driver.find_element(By.XPATH , "//a[contains(text(),'Nonfiction')]")
   # actions = ActionChains(driver)
   # actions.double_click(choose_category_in_list).perform()

   # driver.find_element_by_link_text("Nonfiction").click()
   # choose_category_in_list = driver.find_element(By.XPATH , "//a[contains(text(),'Nonfiction')]").click()
   # choose_category_in_list = driver.find_element(By.XPATH , "//a[contains(text(),'Nonfiction')]").click()  
   element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Nonfiction")))
   element.click()

【问题讨论】:

  • 你能分享一个指向那个页面的链接,或者至少是那个页面的 HTML 吗?不是图片,是代码

标签: python selenium html-select html.dropdownlistfor


【解决方案1】:

根据您的代码,很难做出可靠的答案,但我会尝试几件事。 首先 - 尝试使用

driver.find_element(By.PARTIAL_LINK_TEXT, "your_text")

您可能需要先将元素悬停:

# finds an element and clicks on it
def find_element_hover_click_element(self, *locator):
    element = self.driver.find_element(*locator)
    hover = ActionChains(self.driver).move_to_element(element)
    hover.click().perform()

# hovers over element (need to provide an element, not a locator here)
def hover_click_element(self, element):
    hover = ActionChains(self.driver).move_to_element(element)
    hover.click().perform()

您也可以尝试使用 xpath,但不要瞄准

[//a[contains(text(),'Nonfiction')]

你可能想要找到 DOM 的上层元素

(//div[@id='some_id']//a)

但是如果你在这里发布你的 HTML 代码会更容易

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多