-1

This is the HTML:

<div class="fl">
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Cadastro</h3>
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Movimento</h3>
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Consulta</h3>

I need to click on "Consulta", but I can't. I already tried driver.find_element(By.("CLASS_NAME, XPATH, TAG_NAME, LINK_TEXT,etc") but none is working.

Some elements I've tried:

driver.find_element(BY.XPATH, "//*[@id='conteudo"]/div[2]/div[3]/div[1]/div[3]/h3").click()

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'fl')))).select_by_visible_text("Consulta")

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'colMenu')))).select_by_visible_text("Consulta")

Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'submenu')))).select_by_visible_text("Consulta") 
7
  • Welcome to the community! Thanks for adding the HTML. Can you also provide the exact "CLASS_NAME, XPATH, TAG_NAME, LINK_TEXT" you tried?
    – Lee Jensen
    Feb 6 at 16:53
  • I try: driver.find_element(BY.XPATH, "//*[@id='conteudo"]/div[2]/div[3]/div[1]/div[3]/h3").click()
    – Matheus
    Feb 6 at 17:14
  • Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'fl')))).select_by_visible_text("Consulta")
    – Matheus
    Feb 6 at 17:15
  • Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'colMenu')))).select_by_visible_text("Consulta")
    – Matheus
    Feb 6 at 17:15
  • Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'submenu')))).select_by_visible_text("Consulta")
    – Matheus
    Feb 6 at 17:15

2 Answers 2

0

The HTML code I created as per the information you provided:

<html>
<head>
</head>
<body>
<div class="fl">
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Cadastro</h3>
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Movimento</h3>
<div class="colMenu">
<h3 class="submenu" style="background-position: 0px -68px;">Consulta</h3>
</body>
</html>

It has been automated using Java, Selenium and TMT Testing Utilities library with this code:

package Demo;

import org.openqa.selenium.WebElement;
import com.theminitools.TestingUtilities.Utilities;

public class Demo implements Utilities{
    public static void main(String[] args) {
        try {
            // System.setProperty("webdriver.firefox.driver", "/home/staah/Tools/Java/geckodriver");
            u.setBrowser("chrome");
            u.go("file:///home/milin/eclipse-workspace/Demo/resource/index.html");
            u.wait(5);

//          // your code goes here
            WebElement menuLink = u.find("xpath", "//div[@class='colMenu']/h3[@class='submenu' and contains(text(),'Consulta')]");
            u.click(menuLink);
//          // your code goes here
            
            u.closeBrowser();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I was able to get the script to click the 'Consulta' menu item.

You can translate this code to Python. It should be something like this,

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("file:///home/milin/eclipse-workspace/Demo/resource/index.html")

menu_link = driver.find_element_by_xpath("//div[@class='colMenu']/h3[@class='submenu' and contains(text(),'Consulta')]")
menu_link.click()

driver.close()

I'm not sure of Python as I haven't worked with it.

0
0

Can you try the below XPath expression:

//h3[contains(text(),"Consulta")]

Assumption: The text Consulta appears only once in the website

XPath explanation: This relative XPath searches all the h3 tags which contain text Consulta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.