单击下拉列表不执行



在此处输入图像描述我是带硒的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()

根据您的代码,很难给出一个可靠的答案,但我会尝试几件事。首次尝试使用

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代码放在这里会更容易

相关内容

  • 没有找到相关文章

最新更新