ChrisOdney
ChrisOdney

Reputation: 6384

Webdriver to open a mail in Gmail

I have started using Webdriver to automate our testing which is 100% manual.

In one of the use cases I need to click on a link sent to the user's inbox. I am facing problems with Gmail in this case. After logging in I am not able to figure out how to open a particular email.

I know the email subject etc but I am unable use it to access the element. Gmail receives all its data as JSON and then build the entire page through js functions. So the webdriver is not able to access any of the elements built using the JSOn data received.

Any help is greatly appreciated.

Thanks, Chris.

Upvotes: 3

Views: 19517

Answers (9)

Ankit Tiwari
Ankit Tiwari

Reputation: 51

Try this out , working perfectly for me. This will select random emails, you can also modify as your requirement

    for i in xrange(int(num)):
    time.sleep(3)
    m=random.randint(1,10)
    print("Mail Number "+str(m)+" is selected")
    browser.find_element_by_xpath("//div[@role='tabpanel'][1]//table//tr"+str([m])).click()
    time.sleep(3)
    browser.find_element_by_xpath('//*[@id=":5"]/div[2]/div[1]/div/div[1]/div/div/div').click()

Upvotes: 0

Mayur Kore
Mayur Kore

Reputation: 1

You can use this also if you want to open a particular mail in gmail:

driver.findElement(By.xpath("//tr[i[td[4[div[contains(@class,'yW')]]]")).click();

Here i is the mail number which you want

Upvotes: -1

MCKiran
MCKiran

Reputation: 57

The below selenese command would do:

clickAt | //table/tbody/tr/td[5]/div[@class='yW'] |

Click at the FROM field of first/recent/top most mail to go to mail detail page. // note: tr for first mail, tr[2] for second and so on.

Upvotes: 0

Arian Al Lami
Arian Al Lami

Reputation: 937

The current locator for gmail body is:

driver.findElement(By.className("LW-avf")).click();
driver.findElement(By.className("LW-avf")).clear();
driver.findElement(By.className("LW-avf")).sendKeys("your body message");

Upvotes: 0

Sriram Angajala
Sriram Angajala

Reputation: 21

The above answer is correct to identify a mail in Gmail if we replace the subject. I have tried with Selenium IDE to find the object with the target as

xpath=//div [@class='y6']/span[contains(.,'<your original search text>')]

The object was found but click is not opening the mail.

So after some investigation I found that mouseDown method is working with the above xpath to open a mail. So command will be

selenium.mouseDown("xpath=//div [@class='y6']/span[contains(.,'<your original search text>')]"); 

or

<td>mouseDown</td>
<td>xpath=//div[@class='y6']/span[contains(.,'£10 OFF when you spend £30 or more online')]</td>
<td></td>

in the IDE.

Hope this helps.

Upvotes: 0

Nimeshkumar Prajapati
Nimeshkumar Prajapati

Reputation: 41

In my case, I found the solution by using Action class of Web driver

Pre-requisite: Your driver needs to move to specific frame to locate element

wd.switchTo().frame("canvas_frame");

Step 1) Search for specific email that is created/generated using below code

String searchvalue="html/body/div[1]/div[2]/div/div[1]/div[3]/div/div[1]/div[2]/div[2]/div/form/fieldset[2]/div/div/div[2]/input";
wd.findElement(By.xpath(searchvalue)).sendKeys(sendkeys);
String clickSearch=".//*[@id='gbqfb']";
wd.findElement(By.xpath(clickSearch)).click();

Step 2) Now use Actions class to navigate through.

Actions action= new Actions(wd);
    action.click(firstrecord).build().perform();

Hope this helps out!

Upvotes: 1

nilesh
nilesh

Reputation: 14289

I would suggest NOT to use UI to verify Gmail. Gmail's UI is extremely complicated and it's a trap. To me, automating with selenium is not a solution at all.

Consider using JavaMail API and HTTPURLConnection. This is what I do in a similar testing scenario.

While running the webdriver test, after doing certain action if you expect an email then using JavaMail API poll (for a certain timeout if its not immediate) for the email in the background with certain 'subject' or 'sender' etc. Once the email is found then grab the link from the email content and then simulate a click using HTTPURLConnection

Upvotes: 11

Yuri Vrancken
Yuri Vrancken

Reputation: 21

If you can search the specific email you can use the following code to locate the email you are looking for:

//div [@class='y6']/span[contains(.,'<your original search text>')]

mind that google will cut off the subject which results in something like 'subject...' if the subject is too long. We use a unique number to identify messages in our automated test environment.

Upvotes: 2

9ikhan
9ikhan

Reputation: 1177

I don't think I got your question correctly but I suppose you are having trouble finding the locator to open the mail after you've entered text in search box of gmail.

//div[5]/div/div/table/tbody/tr[n]" //n is the row no. of mailbox, for first result use 1 and like

use this as identifier for element before cliking on it.

Hope this helps.

Upvotes: 1

Related Questions