academica
academica

Reputation: 333

Selenium driver doesn't click on element sometimes

currently i'm writing a web scraper that will work in infinite loop. It gets a page, searches for some buttons and clicks one of them. But sometimes it doesn't! I save a screenshot in case of some fail and it showed me that page didnt changed after button clicked.

driver.find_element_by_xpath('//input[@name = "btn"]').click()
time.sleep(3)

I have bypassed this with a loop checking does we see that element still.

while driver.find_elements_by_xpath('//input[@name = "Submit"]') != []:
    driver.find_element_by_xpath('//input[@name = "Submit"]').click()

But hope to find a root cause of this. What it could be?

Upvotes: 2

Views: 2921

Answers (1)

Nitesh
Nitesh

Reputation: 187

I also faced a similar problem with my application. Clicking the element through action class worked for me. Here is the sample code in Java:

WebElement webElement = driver.findElement(By.id("Your ID Here"));
Actions builder = new Actions(driver);
builder.moveToElement(webElement).click(webElement);
builder.perform();

If clicking with action class does not work, you can also try clicking element by Javascript.

WebElement webElement = driver.findElement(By.id("Your ID here"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", webElement);

Upvotes: 2

Related Questions