简体   繁体   English

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

[英]Python - find element if the previous contains specific type value in xpath

I was struggling to find a way to match an element if there is a previous element that contains a specific name. 如果有一个包含特定名称的前一个元素,我很难找到匹配元素的方法。

Let's take as an example the following HTML: 我们以下面的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>

I want to click on the element of type="submit" (instead of button tag there can be anything) if it is after an element that contains name="email" or name="name" . 如果它位于包含name="email"name="name"的元素之后,我想点击type="submit"的元素(而不是button标记可以有任何内容)。

To match name and email and complete them with some values I have this: 为了匹配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

I am kindly asking for your help as there are more than 10 days I am fighting with this situation. 我很友好地请求你的帮助,因为我有10多天的时间来应对这种情况。

I think you want something like this: 我想你想要这样的东西:

//*[@type="Submit"][preceding::*[@name="email" or @name="name"]]

Of course if you want it to match case-insensitively, then you'll need to do the translate(…) thing for "Submit" and "email" and "name" : 当然,如果你希望它不区分大小写,那么你需要为"Submit""email""name"translate(…)事情:

//*[@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = "submit"]][preceding::*[@name[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="email" or translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') ="name"]]]

这是另一个你可以尝试的

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

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

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