Search code examples
pythonselenium-webdriverwebautomation

Unable to highlight a link on a webpage and then select from the dropdown


After opening the www.netiq.com link, I am trying to highlight Support and select Documentation from the dropdown using selenium webdriver.

HTML code:

<ul>
<li id="nav_solutions" class="large">
<li id="nav_products" class="large">
<li id="nav_industry" class="">
<li id="nav_service" class="active hover">
<a id="hdr_support_main" href="/services/" onclick="ga('send', 'event', 'header', 'support', 'main');" data-di-id="#hdr_support_main">
<strong>Support</strong>
<i class="downarrow"/>
<i class="arrow"/>
</a>
<div style="display: block;">
<ul>
<li>
<li>
<li>
<li>
<li>
<li>
<a id="hdr_support_documentation" href="https://www.netiq.com/documentation/" onclick="ga('send', 'event', 'header', 'support', 'documentation');" data-di-id="#hdr_support_documentation">
Documentation
<i class="arrow"/>
</a>
</li>

I tried writing this:

from selenium import webdriver
# from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time

driver = webdriver.Firefox()
driver.get("https://www.netiq.com/")
time.sleep(5)
# select=Select(driver.find_element_by_xpath(".//*[@id='hdr_support_main']"))
# driver.find_element_by_xpath(".//*[@id='hdr_support_documentation']").click()

select = Select(driver.find_element_by_id("hdr_support_main"))

select.select_by_visible_text("hdr_support_documentation")

Error: Message: Select only works on elements, not on

Please help.


Solution

  • Before you have to move the cursor in the "Support" element, with:

    ActionChains(driver).move_to_element(support).perform()
    

    The entire code:

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://www.netiq.com/")
    support= driver.find_element_by_xpath("//*[@id='nav_service']")
    ActionChains(driver).move_to_element(support).perform()
    
    wait = WebDriverWait(driver, 20)
    docButton= wait.until(EC.element_to_be_clickable((By.XPATH,"//*[@id='hdr_support_documentation']")))
    
    docButton.click()