0

This is the Url: http://automationpractice.com/index.php

I want to select "casual dresses", but when I tried the "Women" WebElement is selected instead.

How can I write the dyanmic xpath to select "casual dresses"?

2
  • Did you try my answer? Commented Jun 16, 2021 at 14:39
  • Yes, but i got Syntax error at visibilityOf method, it's showing like"The method visibilityOf(WebElement) in the type ExpectedConditions is not applicable for the arguments (By)"-@Prophet Commented Jun 16, 2021 at 15:05

3 Answers 3

1

You have to wait until page is loaded, then hover over Women title, the wait until the menu is open and then click on the Casual Dress item.
It's best practice to keep variables in the top of class, not inside the code hardcoded.
Try this:

//Define expected conditions explicitly wait
WebDriverWait wait = new WebDriverWait(driver, 30);
//Define Actions object instance
Actions action = new Actions(driver);
//define locators
String womenTitleXpath = "//a[@title='Women']";
String up = "/..";
String womenCasualDressXpath = womenTitleXpath + up + "//a[@title='Casual Dresses']";
//Wait until the element is visible
wait.until(ExpectedConditions.visibilityOf(By.xpath(womenTitleXpath)));
WebElelemnt womenTitle = driver.findElement(By.xpath(womenTitleXpath));
//Hover over the Menu to open it
action.moveToElement(womenTitle).build().perform();
//Wait until the menu item is visible
wait.until(ExpectedConditions.visibilityOf(By.xpath(womenCasualDressXpath)));
//Finally click on the desired element
driver.findElement(By.xpath(womenCasualDressXpath)).click();

Sign up to request clarification or add additional context in comments.

11 Comments

and cruisepandey what effort this user has put that you decided to give out answers? Just curious to know
Just trying to help, without additional thoughts.
Does this rule applies to everybody?
Mainly yes, until I see person who is known as unthankful.
Also, I do not know everything, so I answer only when I know or at least think it will be helpful.
|
0

Here is a quick python script that clicks that "Casual Dresses" button. You have to use ActionChains to hover over "Women", then identify and click "Casual Dresses":

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://automationpractice.com/index.php")

womenElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[title="Women"]')))
ActionChains(driver).move_to_element(womenElement).perform()
casualDressesElement = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[title="Casual Dresses"]')))
casualDressesElement.click()
time.sleep(5)
driver.quit()

2 Comments

OK, in the future please include your code in any question, and specify the coding language. In the mean time, check out @Prophet 's solution :)
Thank you for your code, yeah compulsory i do it next time when i want to post any question.
0

You can find by reading the <li> items,this is my result:

xpath:

//*[@id="block_top_menu"]/ul/li[1]/ul/li[2]/ul/li[1]/a

full xpath:

/html/body/div/div[1]/header/div[3]/div/div/div[6]/ul/li[1]/ul/li[2]/ul/li[1]/a

2 Comments

I tried above Xpath but i get ElementNotInteractableException Error..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.