Home > Back-end >  How to locate an element by using any of the selenium web driver locators
How to locate an element by using any of the selenium web driver locators

Time:09-17

Website URL: https://staging.corporatehousingbyowner.com/

Hi, I am unable to locate and click at the following button present in a UL > li through Selenium Webdriver with Java. Using Chrome driver to perform automated execution.

<ul>
<li><a class="list-your-prop" href="/dashboard/properties/add/">List Your Property</a></li>
<ul/>

Already tried the following solutions.

1:

WebElement element = driver.findElement(By.linkText("List Your Property"));
        element.click();

2:

WebElement element = driver.findElement(By.className("menu-text"));
String elementText = element.getText();

3:

WebElement element = driver.findElement(By.xpath("//li[contains(@class,'list-your-prop')]/ul/li/a"));
        element.click();

4: driver.findElement(By.cssSelector("body > header:nth-child(1) > nav:nth-child(4) > div:nth-child(1) > div:nth-child(2) > ul:nth-child(2) > li:nth-child(4) > a:nth-child(1)")).click();

[WebPage Highlights][1] [1]: https://i.stack.imgur.com/FH2LR.png

Please help, thanks in advance.

CodePudding user response:

There are two web elements with List Your Property.

The below xpath has unique entry in HTMLDOM

//div[@id='myNavbar']/descendant::a[text()='List Your Property']

Code trial 1 :

WebElement element = driver.findElement(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']")); 
element.click();

Code trial 2 : With Explicit waits:

WebElement element  = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']")));
element.click();

Update 1 :

driver.manage().window().maximize();
//WebDriverWait wait = new WebDriverWait(driver, 30);
//driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);
driver.get("https://staging.corporatehousingbyowner.com/");
WebElement element  = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='myNavbar']/descendant::a[text()='List Your Property']")));
element.click();

CodePudding user response:

You can use

driver.findElement(By.cssSelector("nav.navbar a.list-your-prop")).click();

It is also better to use waiters because the element might take time to load.

  • Related