0

I believe I have the same logic in Python and Java, with the same xpath to a button (picture below).

Python code:

button = driver.find_element(By.XPATH, "//*[@id='tool_form']/div/div[1]/div/button")
button.click()

Java code:

WebElement button = driver.findElement(By.xpath("//*[@id='tool_form']/div/div[1]/div/button"));
button.click();

enter image description here

The button gets clicked using my Python code, but not my Java code. What could I be doing wrong?

Frank G
  • 49
  • 6
  • is the page loaded in Java vs. python? What happens if you try to get the text content from the button? (does the Java version still get the content)? – Andrew Ryan Mar 16 '23 at 20:38

1 Answers1

0

To click on the clickable element ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

  • Using Java client:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='tool_form']/div/div[1]/div/button"))).click();
    
  • Using _Python_client:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='tool_form']/div/div[1]/div/button"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352