Python - 如果前一个包含xpath中的特定类型值,则查找元素

时间:2015-08-31 06:29:11

标签: python selenium xpath selenium-webdriver webdriver

如果有一个包含特定名称的前一个元素,我很难找到匹配元素的方法。

我们以下面的HTML为例:

<div>
    <input type="text" name="name">
</div>
<div>
    <input type="text" name="email">
</div>
<div>
    <input class="bla">
</div>
<!-- there might be more divs between the email / name and the button that I want to press-->
<div>
    <button class="something" type="Submit"></button>
</div>

如果它位于包含type="submit"button的元素之后,我想点击name="email"的元素(而不是name="name"标记可以有任何内容)。

要匹配nameemail并使用某些值完成它,我会这样:

# some variables and booleans defined here
# (...)


driver = webdriver.Firefox()

lista = [
    'https://rapidevolution.clickfunnels.com/jv-page-2',
    'http://Listhubpro.com/jv',
    'http://viralautopilotfunnels.com/jv',
    'http://giantvideokit.com/vol3jv',
    'http://www.thefivedalliance.com/jv',
    'http://CPAApex.com/jv/',
    'http://www.wpproassistant.com/partners/',
    'http://domainerelite.com/',
    'http://socialsurveys.io/jv/',
    'http://tubeviperx.com/jvinvite',
    'http://svensroadtoimsuccess.com/affiliates',
]

for url in lista:
    not_found = False
    name_required = True
    email_required = True
    button_required = True

    driver.get(url)
    time.sleep(2)

try:
        name_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
        name_box.click()
        name_box.clear()
        name_box.send_keys('MyName')
    except:
        not_found = True

    try:
        email_box = driver.find_element_by_xpath("//input[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
        email_box.click()
        email_box.clear()
        email_box.send_keys('email@yahoo.com')
    except:
        not_found = True

    if not_found:
        print "here"
        for element in driver.find_elements_by_xpath("//input[@type='text']"):
            if name_required:
                try:
                    name_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'name')]]")
                    name_box.click()
                    name_box.clear()
                    name_box.send_keys('MyName')
                    name_required = False
                    continue
                except:
                    pass

            if email_required:
                try:
                    email_box = element.find_element_by_xpath(".[@*[contains(translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'email')]]")
                    email_box.click()
                    email_box.clear()
                    email_box.send_keys('email@yahoo.com')
                    email_required = False
                    break
                except:
                    pass

            if (not name_required) and (not email_required) and (not button_required):
                break
# from here, the code is not doing what I want

    for element1 in driver.find_elements_by_xpath("//*[@type='submit'][preceding-sibling::email]"):
        if button_required:
            try:
                button = element1.find_element_by_xpath("//*[@type='submit'][preceding-sibling::email]").click()
                element1.click()
                element1.send_keys(Keys.ENTER)
                #button_required = False
                continue
            except:
                try:
                    element1.find_element_by_xpath('/a').click()
                    #button_required = False
                except:
                    pass

    time.sleep(2)
    print button_required

我很乐意寻求你的帮助,因为我在这种情况下已经有超过10天的时间了。

2 个答案:

答案 0 :(得分:2)

我想你想要这样的东西:

translate(…)

当然,如果您希望它不区分大小写,那么您需要为"Submit""email"以及"name"执行//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = "submit"]][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="email" or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="name"]]] 事项:

select *, out("DirectedBy").Name, out("Starring").Name
from (
  select expand( in("DirectedBy") ) from person where Name lucene 'John Ford'
)

答案 1 :(得分:1)

这是另一个你可以尝试的

//*[@name='email' or @name='name']//following::*[@type='submit']