Answer the question
In order to leave comments, you need to log in
How to count emails using Selenium/Java?
The test task came two weeks ago. I didn’t understand why it came to me, so I didn’t lose time for a long time, I sat for 2 hours and didn’t answer it. Generally refused.
It was necessary to use Selenium, which I first heard about. Open mail, count letters, close mail.
In general, in order to distract from my studies, I decided to just try to overcome him, and for the second night I have been sitting and I don’t understand ...
How to count the letters in the mailbox by the subject of the letter. The output was either 0 (zero), or 1, or nothing. I even got to the bottom of the sharp developer, he even threw me his code, where he succeeded and everything was considered. That's just it has a .Count method, and I should have, as I understand it, .size (). And it doesn't work.
I will not throw off all 20 attempts to solve the issue, here is the last option (similar to Sharp's assistant) ... (sorry for the xPath curve, there is no time to figure it out, and it is unlikely to be needed in the near future).
Actually the counting code itself (after entering the box):
driver.findElement(By.xpath("//*[@id=\"js-apps-container\"]/div[2]/div[8]/div/div[2]/div/div/div[1]/div[2]/div/div/div/div[1]/form/div/span/input")).click();
driver.findElement(By.xpath("//button[@title='расширенный поиск']")).click();
var el = driver.findElement(By.xpath("//span[text()='Ещё']"));
el.findElement(By.xpath("./..")).findElement(By.xpath("./..")).click();
el = driver.findElement(By.xpath("//span[text()='В теме письма']"));
el.findElement(By.xpath("./..")).findElement(By.xpath("./..")).click();
driver.findElement(By.xpath("//*[@id=\"js-apps-container\"]/div[2]/div[8]/div/div[2]/div/div/div[1]/div[2]/div/div/div/div[1]/form/div/span/input")).click();
driver.findElement(By.xpath("//*[@id=\"js-apps-container\"]/div[2]/div[8]/div/div[2]/div/div/div[1]/div[2]/div/div/div/div[1]/form/div/span/input")).sendKeys("Тема письма");
el = driver.findElement(By.xpath("//span[text()='Найти']"));
el = el.findElement(By.xpath("./.."));
el.click();
var count = driver.findElements(By.xpath("//span[@title='Тема письма']")).size();
System.out.println(count);
Answer the question
In order to leave comments, you need to log in
Sorry for being abstract, because the code would still be incredibly different since I last worked on selenium in python
1. Since mail is most likely built on a framework (react / vue / angular), it builds elements dynamically, therefore your 1st step should be that you should set some wait/until in the driver. That is, you have to wait until the page is loaded.
// Ждём, пока не будет элемента с name = q
WebElement foo = new WebDriverWait(driver, Duration.ofSeconds(3))
.until(driver -> driver.findElement(By.name("q")));
Good afternoon.
Well, the answer to your question can be very different from mail provider to mail provider, because they have a different DOM and, accordingly, different selectors (xpath) will be needed.
In simple terms, then:
- open a browser (you can use it in headless mode)
- find a node that contains a list of letters
- select this node. Selenium has a class called WebElement. Since there are several elements, use List<WebElement>
.
- Since we have a List, we call the size() method and get the number of elements placed in the list.
And as a colleague correctly noted, rst must be made to wait for the page to load. Read about Explicit & Implicit Waits
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question