Python Selenium ---如何支持Gmail在一个选项卡下撰写多个具有自定义主题和正文内容的新邮件(Gmail)

时间:2018-06-22 00:31:13

标签: python selenium

我是Python和Selenium学习的新手

我编写了如下脚本来撰写1条消息然后发送给它

预期:我想在一个标签下撰写50条以上的邮件,并自定义主题和正文内容

我的问题:这些新消息框具有相同的类名,我如何识别每个

谢谢

browser=webdriver.Chrome(exepath)
browser.get(url)

elem = browser.find_element_by_id('identifierId')
elem.send_keys(user)

browser.find_element_by_xpath ('//*[@id="identifierNext"]/content/span').click()
time.sleep(2)


elem = browser.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input')
elem.send_keys(pwd)
time.sleep(2)

browser.find_element_by_xpath ('//*[@id="passwordNext"]/content/span').click()
time.sleep(10)


browser.find_element_by_xpath (".//*[text()= 'COMPOSE']").click()
time.sleep(2)

elem = browser.find_element_by_class_name("vO")
elem.send_keys(to)
time.sleep(3)

elem = browser.find_element_by_class_name("aoT")
elem.send_keys(subject)
time.sleep(3)

#browser.find_element_by_xpath("//div[text()='Send']").click()

1 个答案:

答案 0 :(得分:0)

每个框架都有自己的带有标识符的div吗? 您可以获得所有这些框架:

frames = browser.find_element(<selector>)
for frame in frames:
    # your code here, you can use XPATH only selectors to search in parent element, just use '.' at the beggining of each, example method is below

获取每个子元素的方法:

_xpath_test_elem_parent = ".//*{0}"

def parent_element(self, parent: WebElement, locator: str):
    """
    Returns ``WebElement`` instance for the given data-test locator and
    parent element

    :param parent: Parent element
    :param locator: Data-test locator for element
    :return: WebElement instance
    """
    logger.info('Searching for element with '
               '"{0}"'.format(locator))
    elem = parent.find_element_by_xpath(
        self._xpath_test_elem_parent.format(locator))
    return elem