ctina
ctina

Reputation: 51

Selenium search text in a page section and click on a button inside it

I am using Webdriver and Java. My problem is that the page is splitted in 2 sections, both with the same name //div[@class='sg-content-box sg-content-box--spaced']

I want to click on the button Aprobă near the user David2211 (his answer can appear first or second)

My problem is that i don't know how to do click on the button in that specific area, for that specific username

enter image description here

Upvotes: 2

Views: 1992

Answers (5)

undetected Selenium
undetected Selenium

Reputation: 193088

Here is the Answer to your Question:

To click on the button Aprobă near the user David2211 (his answer can appear anywhere) you can use the following code block:

JavascriptExecutor je = (JavascriptExecutor)driver;
WebElement element = driver.findElement(By.xpath("//a[contains(text(),'David2211')]//preceding::button[2]/div/div[contains(text(),'Aproba')]"));
je.executeScript("arguments[0].scrollIntoView(true);",element);
element.click();

Let me know if this Answers your Question.

Upvotes: 0

JeffC
JeffC

Reputation: 25597

For things like this, you have to use XPath since we are looking for an element that contains specific text. In this case, we will be looking for an element that contains the username 'David2211' and then a button that contains the text 'Aprobă'. I would assume that you will use this code more than once using different usernames so I would put this in a function so that it's easier to reuse.

The function

public void approveAnswerByUsername(String username)
{
    driver.findElement(By.xpath("//a[contains(.,'" + username
            + "')]/ancestor::div[contains(@class, 'js-answer-element')]//div[contains(@class,'js-approve-button-text')][contains(.,'Aprobă')]")).click();
}

and you call it like

approveAnswerByUsername("David2211");

This XPath is rather large and complicated but it looks for an A tag that contains the specified username, goes up the DOM to find a parent DIV that encloses the entire answer and then navigates back down the DOM to find a DIV that contains the 'Aprobă' text.

Upvotes: 1

santhosh kumar
santhosh kumar

Reputation: 2019

A challenging xpath after many days. Thanks for asking this question. Try this below xpath.

"//li/a[contains(text(),'DemonBolt')]/ancestor::div[@class='sg-content-box sg-content-box--spaced']//div[@class='sg-label__text js-approve-button-text']"

or

 "//li/a[contains(text(),'DemonBolt')]/ancestor::div[@class='‌​sg-content-box sg-content-box--spaced']//div[contains(text(),'Aprobă')]" 

Just change the text DemonBolt with the name whom you need to approve.

Hope this helps. Thanks.

Upvotes: 0

PIERRE COIFFIER
PIERRE COIFFIER

Reputation: 41

The only thing specific to your button is indeed the username. I would suggest to get the xpath of the username's node, and then try to get its button from the parent's node using the xpath :

//{username's xpath}/parent::div[@class='sg-content-box sg-content-box--spaced']/{button's xpath}

It seems quite barbaric but that's the only way I see.

Upvotes: 0

NarendraR
NarendraR

Reputation: 7708

Try Below xpath to locate the same element

Here you have to pass the user's name whose button you wants to click

//li/a[contains(.,'David2211')]/../../../preceding-sibling::div//button[@title='Aprobă']

Upvotes: 0

Related Questions