如何通过XPath获取指定Button的上级<a>元素href属性
问题:获取嵌套按钮对应外层标签href属性失败
需要获取点击按钮后跳转的源URL,但按钮嵌套在最外层标签的多层子元素中。使用get_attribute("href")无效,因为当前代码里find_element(By.XPATH, "..")仅能拿到直接父元素,而非最外层的,导致cta_url返回None。
原代码如下:
for i in range(20): starting_element.send_keys(Keys.TAB) tab_wait = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//body/div/div/div[@role='main']/div/div/div/div/div/div/div[4]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]"))) starting_element = browser.switch_to.active_element time.sleep(1) # check for a set of keywords when a CTA button is targeted, if matched then extract URL from source if starting_element.aria_role == 'button': button_text = starting_element.text if button_text in meta_cta_buttons: parent_element = starting_element.find_element(By.XPATH, "..") cta_url = parent_element.get_attribute('href') print(cta_url) else: continue
对应HTML结构:
<a class="x1hl2dhg x1lku1pv x8t9es0 x1fvot60 xxio538 xjnfcd9 xq9mrsl x1yc453h x1h4wwuj x1fcty0u x1lliihq" target="_blank" href="https://l.facebook.com/l.php?u=https%3A%2F%2Fsalankaracreation.com%2Fproducts%2Fsalankara-creation-designer-drop-fitted-naksha-bala%3Futm_content%3DFacebook_UA%26utm_source%3Dfacebook%26variant%3D42342785024177%26fbclid%3DIwAR2lR9RPGQqnisFtySxqLr6BZgNmLU-TdRsT-SOF2h4syzfIAoY28E8SX5M&amp;h=AT3CWaIDaXpKVTEKF3uYuZ56OpneGn978po8Fmdt1K4Dzfb6w_iVhgSiF1tuBAoze9JSfPDOhbiDQa5zbgUVLGj75QM6o4HQhgtqPazAFRo7mRFx43-qk0J84U2Xxf78QTw" rel="nofollow noopener" data-lynx-mode="hover" data-lynx-uri="https://l.facebook.com/l.php?u=https%3A%2F%2Fsalankaracreation.com%2Fproducts%2Fsalankara-creation-designer-drop-fitted-naksha-bala%3Futm_content%3DFacebook_UA%26utm_source%3Dfacebook%26variant%3D42342785024177%26fbclid%3DIwAR0F44dXcdztlWq0w3l55eFaO6lkc8T-abjcok_Z4YRosIEFo7nVRvHP1zI&amp;h=AT3CWaIDaXpKVTEKF3uYuZ56OpneGn978po8Fmdt1K4Dzfb6w_iVhgSiF1tuBAoze9JSfPDOhbiDQa5zbgUVLGj75QM6o4HQhgtqPazAFRo7mRFx43-qk0J84U2Xxf78QTw" style="" xpath="1"> <div class="x1ywc1zp x78zum5 xl56j7k x1e56ztr x1277o0a"> <img alt="" class="x1ll5gia x19kjcj4 xh8yej3" referrerpolicy="origin-when-cross-origin" src="https://scontent-ord5-1.xx.fbcdn.net/v/t39.35426-6/329103301_714332536939836_1381434036214193350_n.jpg?stp=dst-jpg_s600x600&amp;_nc_cat=109&amp;ccb=1-7&amp;_nc_sid=cf96c8&amp;_nc_ohc=UhFGxKsW874AX9Xc-Ej&amp;_nc_ht=scontent-ord5-1.xx&amp;oh=00_AfAW7WlYCfzcgAaYzDOUzkJtPa1u8nYcwC9ARHVfbWmKJQ&amp;oe=63ECBC09"> </div> <div class="_8jgz _8jg_"> <div class="x1iyjqo2 xw3qccf x6ikm8r x10wlt62 xt0b8zv"> <div class="_8jh5"> <div tabindex="0" role="button"> <div class="_4ik4 _4ik5" style="line-height: 12px; max-height: 24px; -webkit-line-clamp: 2;">SALANKARACREATION.COM</div> </div> </div> <div class="_8jh2"> <div tabindex="0" role="button"> <div class="_4ik4 _4ik5" style="line-height: 14px; max-height: 28px; -webkit-line-clamp: 2;">Salankara Creation Designer Drop Fitted Naksha Bala</div> </div> </div> <div class="_8jh3"> <div tabindex="0" role="button"> <div class="_4ik4 _4ik5" style="line-height: 14px; max-height: 28px; -webkit-line-clamp: 2;">₹1,449</div> </div> </div> <div class="_8jh4"> <div tabindex="0" role="button"> <div class="_4ik4 _4ik5" style="line-height: 12px; max-height: 24px; -webkit-line-clamp: 2;"></div> </div> </div> </div> </div> <div class="_8jh0"> <div class="x3nfvp2 x193iq5w xxymvpz" role="none" style="max-width: 120px;"> <div aria-busy="false" class="x1i10hfl xjqpnuy xa49m3k xqeqjp1 x2hbi6w x972fbf xcfux6l x1qhh985 xm0m39n x9f619 x1ypdohk xe8uvvx xdj266r x11i5rnm xat24cr x1mh8g0r x2lwn1j xeuugli x16tdsg8 xggy1nq x1ja2u2z x1t137rt x6s0dn4 x1ejq31n xd10rxx x1sy0etr x17r0tee xdl72j9 x1q0g3np x193iq5w x1n2onr6 x1hl2dhg x87ps6o xxymvpz xlh3980 xvmahel x1lku1pv xhk9q7s x1otrzb0 x1i1ezom x1o6z2jb xo1l8bm x108nfp6 xas4zb2 x1y1aw1k xwib8y2 x1swvt13 x1pi30zi x78zum5 x1iyjqo2 xs83m0k" role="button" tabindex="0" style=""> <span class="x8t9es0 x1fvot60 xxio538 x1heor9g xq9mrsl x1h4wwuj x1pd3egz xeuugli xh8yej3"> <div class="x78zum5"> <div class="xeuugli x2lwn1j x6s0dn4 x78zum5 x1q0g3np x1iyjqo2 xozqiw3 x19lwn94 x1hc1fzr x13dflua x6o7n8i xxziih7 x12w9bfk xl56j7k xh8yej3"> <div class="x8t9es0 x1fvot60 xxio538 x1heor9g xuxw1ft x6ikm8r x10wlt62 xlyipyv x1h4wwuj x1pd3egz xeuugli">Shop Now</div> </div> </div> </span> </div> </div> </div> </a>
解决方法
- 放弃仅获取直接父元素的方式,使用XPath的
ancestor::a语法,直接定位当前元素的所有<a>标签祖先节点,取第一个即为最外层的<a>。 - 修改获取父元素的代码逻辑,直接通过祖先定位拿到目标标签。
修改后的代码:
for i in range(20): starting_element.send_keys(Keys.TAB) tab_wait = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, "//body/div/div/div[@role='main']/div/div/div/div/div/div/div[4]/div[1]/div[1]/div[1]/div[1]/div[1]/div[2]/div[1]"))) starting_element = browser.switch_to.active_element time.sleep(1) # 匹配CTA按钮关键词后提取源URL if starting_element.aria_role == 'button': button_text = starting_element.text if button_text in meta_cta_buttons: # 定位当前元素的第一个a标签祖先 parent_a = starting_element.find_element(By.XPATH, "./ancestor::a[1]") cta_url = parent_a.get_attribute('href') print(cta_url) else: continue
内容的提问来源于stack exchange,提问作者MacEng
相关产品推荐
相关产品推荐

