简体   繁体   中英

Java selenium click element not working

I want to click the load More link in a page. My code is below.

pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"

WebDriver driver = new FirefoxDriver();

driver.get(pageUrl);

driver.manage().window().maximize();

JavascriptExecutor jse = (JavascriptExecutor) driver;

jse.executeScript("window.scrollBy(0,2500)", "");

WebDriverWait wait = new WebDriverWait(driver, 60);

wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));

driver.findElement(By.id("json_click_handler")).click();

How can I make it click the link.

您可以使用xpath下面的两次来单击Load More按钮:

driver.findElement(By.xpath("//*[@id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();

this button change the location after you click on it and it can be clicked twice so:

before the first click use

driver.findElement(By.xpath("//*[@id="blocks-left"]/div/div[3]/div")).click();

after first click,you can use

driver.findElement(By.xpath("//*[@id="blocks-left"]/div/div[3]/div[2]")).click();

Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM