0

In my java and Selenium project, I have the below method to click on a specific element

public void click(String xPath) {
     driver.findElement(By.xpath(xPath)).click();
}

Currently, i have an element that it does not react on my click! So, i need to make sure, if selenium really successfully click on this element.

Point: There is no error during the runtime. So, i think it can find the element. But, it is a surprise for me, that there is no effect of click!


More details:

I am trying to automate this page. You can find the complete code in this repository. You need to take a few steps till you reach to the point that i really have the problem. That's why i share the repository, since the page is not directly accessible (sorry for that).

When i am in the https://hello.friday.de/quote/selectFuelType, (please find the image)

enter image description here

I am not able to click on the item (Benzin) with the below xpath:

final String fuel = "//*[@id=\"root\"]/div/div[3]/div/div[2]/div/div/form/div[2]/div[2]/button[1]";

During the runtime, there is no error, so, i expect that I can successfully click on the element, or successfully get the current url as /selectFuelType but none of them are working.

The problematic method is the i_am_asked_to_specify_the_Fuel_Type_of_the_car() in the RegisterInsuranceSteps class.

Jeff
  • 7,767
  • 28
  • 85
  • 138

3 Answers3

2

Your xpath can be much simpler and stabler. Try this:

final String fuel =  "//button[contains(., 'Benzin')]"

See more details about contains method at MDN web docs or check this SO answer to see how to check against attributes or tag names.

Regarding verifying if button was really click, I think it's enough if your next test step fails. In your case it would be choosing the engine power.

Tomas
  • 1,849
  • 1
  • 13
  • 13
0

On the website https://hello.friday.de/quote/selectPrecondition the first option Das Auto ist schon versichert is selected by default. So click() the second option Das Auto wird noch zugelassen oder umgemeldet you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • cssSelector:

    WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class^='RadioButtonListField__container--'] button:nth-child(2)"))).click()
    
  • XPATH:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(@class, 'RadioButtonListField__container--')]//following-sibling::button[1]"))).click();
    

Note: These are relative locators as per the relevant HTML and are not hard-coded to the exact text of the options.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Firstly if selenium doesnt click then try to use alternate xpath. You can also use css selectors.

Secondly if you want to check if click has happened and next page is loaded then you can simply check change in title or you can check if some element has loaded from next page.