简体   繁体   English

使用 Selenuim 查找元素 Java

[英]Find Element with Selenuim Java

I started working with Selenium Java. I would like to indicate the search element for a website.我开始使用 Selenium Java。我想指出一个网站的搜索元素。 Whatever Xpath, CssSelector... The site is this https://www.paruvendu.fr/ et i would like to create a car locator ad.无论 Xpath,CssSelector... 该网站是这个https://www.paruvendu.fr/等我想创建一个汽车定位器广告。 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".而且我无法单击它打开它和 select“选择类别”。 And "MOTO-BATEAU" and finally "Used car" Normally, at that point, it's over.还有“MOTO-BATEAU”,最后是“二手车” 通常,到那时就结束了。 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}

I could not understand what you mean by and finally "Used car" .我不明白你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 .此外,我们几乎从不使用By.partialLinkTextBy.linkText It's always better to use XPath, CSS Selector, ID or class name.最好使用 XPath、CSS 选择器、ID 或 class 名称。
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.以下代码打开 web 页面,关闭 cookies 横幅,打开下拉列表,打开“AUTO-MOTO-BATEU”,最后选择“Motos, Scooters et Quads”子类别。
I mostly used Xpathes here since these elements could easily be located based on their texts.我在这里主要使用 Xpathes,因为这些元素可以根据它们的文本轻松定位。
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:这里使用的clickVisible方法实现如下:

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;
    }
}

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:如果是,请尝试使用相对 xPath 而不是使用 linkText 定位器,尝试以下代码:

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

My Code is with the two proposition.我的代码是带有两个命题的。 It's like this就像这样

System.setProperty("webdriver.chrome.driver", "C:\\kafka\\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.xpath("//div[@data-type=1]")).click();
    driver.findElement(By.xpath("//li[contains(text(),'AUTO-MOTO-BATEAU')]")).click();
    driver.findElement(By.xpath("(//li[normalize-space()=\"Voiture d'occasion\"])[1]")).click();

And I want to complete the steps to fill in the fields and publish the ad.我想完成填写字段和发布广告的步骤。 And Thank you #Shawn and #Prophet becouse this code is a mixture of two codes:)谢谢#Shawn 和#Prophet,因为这段代码是两种代码的混合:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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