selenium - Python - find element if the previous contains specific type value in xpath -
i struggling find way match element if there previous element contains specific name.
let's take example following html:
<div> <input type="text" name="name"> </div> <div> <input type="text" name="email"> </div> <div> <input class="bla"> </div> <!-- there might more divs between email / name , button want press--> <div> <button class="something" type="submit"></button> </div>
i want click on element of type="submit"
(instead of button
tag there can anything) if after element contains name="email"
or name="name"
.
to match name
, email
, complete them values have this:
# variables , 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', ] 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" 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) , (not email_required) , (not button_required): break # here, code not doing want 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 kindly asking there more 10 days fighting situation.
i think want this:
//*[@type="submit"][preceding::*[@name="email" or @name="name"]]
of course if want match case-insensitively, you’ll need translate(…)
thing "submit"
, "email"
, "name"
:
//*[@type[translate(., 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz') = "submit"]][preceding::*[@name[translate(., 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz') ="email" or translate(., 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz') ="name"]]]
Comments
Post a Comment