Home > Software design >  Find Element with Selenuim Java
Find Element with Selenuim Java

Time:01-20

I started working with Selenium Java. I would like to indicate the search element for a website. Whatever Xpath, CssSelector... The site is this https://www.paruvendu.fr/ et i would like to create a car locator ad. This is my code

    System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

    ChromeOptions chromeOptions = new ChromeOptions();
    Map<String, Integer> timeOUts = new HashMap<>();
    timeOUts.put("implicit", 1000);
    chromeOptions.setCapability("timeouts", timeOUts);

    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.manage().window().maximize();
    JavascriptExecutor js = (JavascriptExecutor) driver;
    driver.get("https://www.paruvendu.fr/");
    js.executeScript("cmp_pv.cookie.saveConsent(false);");
    driver.navigate().refresh();
    driver.findElement(By.partialLinkText("Déposer une annonce gratuite")).click();     
    driver.findElement(By.linkText("Choisissez une catégorie")).click();

I can access the form to drop off the car. And I can’t click on it to open it and select "Choose a category". And "MOTO-BATEAU" and finally "Used car" Normally, at that point, it’s over. Help me, I’m stuck and I thank you in advance.

i have this message Error

For documentation on this error, please visit: 
https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.7.2', revision: '4d4020c3b7'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: 
'10.0', java.version: '11.0.16'
 Driver info: org.openqa.selenium.chrome.ChromeDriver
 Command: [7d5bbe84f8b9a235778639176345e277, findElement {using=link 
 text, value=Choisissez une catégorie}]
  Capabilities {acceptInsecureCerts: false, browserName: chrome, 
browserVersion: 109.0.5414.75, chrome: {chromedriverVersion: 109.0.5414.74 
(e7c5703604da..., userDataDir: C:\Users\hp\AppData\Local\T...}, 
goog:chromeOptions: {debuggerAddress: localhost:54474}, 
networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: 
WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:54474/devtoo..., 
se:cdpVersion: 109.0.5414.75, setWindowRect: true, 
strictFileInteractability: false, timeouts: {implicit: 1000, pageLoad: 
300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, 
webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}

CodePudding user response:

I could not understand what you mean by and finally "Used car".
It's always recommended to wait for elements to be clickable before you trying to click them.
Also we almost never use By.partialLinkText and By.linkText. It's always better to use XPath, CSS Selector, ID or class name.
The following code opens the web page, closes the cookies banner, opens the drop list, opens "AUTO-MOTO-BATEU", then finally selects "Motos, Scooters et Quads" sub-category.
I mostly used Xpathes here since these elements could easily be located based on their texts.
This is my code:

driver.get("https://www.paruvendu.fr/");
WebDriverUtils.clickVisible(driver,By.xpath("//button[contains(@onclick,'saveConsent(true)')]"));
WebDriverUtils.clickVisible(driver,By.xpath("//div[contains(text(),'Choisissez une catégorie')]"));
WebDriverUtils.clickVisible(driver,By.xpath("//li[contains(text(),'AUTO')]"));
WebDriverUtils.clickVisible(driver,By.xpath("//li[contains(text(),'Motos')]"));

The clickVisible method used here is implemented as following:

public static boolean clickVisible(WebDriver driver, By locator) {
    try {
        Wait<WebDriver> wait = new WebDriverWait(driver, 30);
        wait.until(ExpectedConditions.elementToBeClickable(locator)).click();
        return true;
    } catch (Exception e) {
        ConsoleLogger.error("Failed to click on element "   e.getMessage());
        return false;
    }
}

CodePudding user response:

If my understanding of your issue is correct, your below line of code is not working?

driver.findElement(By.linkText("Choisissez une catégorie")).click();

If yes, instead of using linkText locator, try using a relative xPath, try the below code:

driver.findElement(By.xpath("//div[@data-type=1]")).click();
  • Related