[英]Selenium Python - Can't Find Correct CSS or Xpath in deeply nested HTML Element
我通常對 Python 中的編碼和 Selenium 很陌生,但我正在編寫代碼來自動執行手動任務,我必須使用 LinkedIn Recruiter 來完成工作。
我正在努力讓代碼定位一個動態按鈕/鏈接的元素,這似乎是非常嵌套的(我認為這是正確的術語)。
我需要它來查找的鏈接/按鈕會將您帶到最近查看的項目,並直觀地顯示為項目名稱,鏈接、文本名稱和 ID 會根據您上次查看的項目而變化。
任何人都可以幫助我選擇正確的定位器或 CSS 元素等,或者我應該如何做到這一點? 我已經嘗試了 HTML 的幾乎所有部分,並嘗試了 class、CSS、XPath,但我終其一生都無法解決。
HTML如下:
<div class="project-list-item\_\_project-meta-data" xpath="1">
<div id="ember1159" class="project-lockup t-14 ember-view">
<div class="project-list-item\\\\\\\_\\\\\\\_title-container project-list-item\\\\\\\_\\\\\\\_metadata-row">
<div id="ember1160" class="project-list-item\\\\\\\_\\\\\\\_project-card-link ember-view">
<a href="INTERNAL LINK TO PROJECT LINK" id="ember1161" class="project-list-item\\\_\\\_project-card-link ember-view">
<h1 class="project-list-item\\\\\\\_\\\\\\\_name" data-test-project-name="" data-live-test-project-name="">
<div id="ember1162" class="project-lockup-title t-black ember-view">
<span class="project-lockup-title\\\\\\\_\\\\\\\_item t-bold" data-test-project-lockup-title="" data-live-test-project-lockup-title=""> PROJECT TITLE</span>
我最近獲得它的嘗試如下:
#transfers to LinkedIn
RecruiterRecruiter_Link = driver.find_element_by_css_selector(".global-nav__primary-link.ember-view[target='_blank']")
Recruiter_Link.click()
time.sleep(5)
*這部分工作正常
#Selects First Project
driver.find_element_by_css_selector("div[class='ember-view'] div:nth-child(1) li:nth-child(1) div:nth-child(2) div:nth-child(1) div:nth-child(1) div:nth-child(1) a:nth-child(1) h1:nth-child(1) div:nth-child(1) span:nth-child(1)")
*這部分不起作用
這是錯誤:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element
任何人的想法? 任何幫助將不勝感激!!
歡迎。
我對 Python 一無所知,但 CSS 選擇器可以像這樣縮短:
driver.find_element_by_css_selector("div.project-lockup.t-14.ember-view > div > div.ember-view > a.ember-view > h1 > div.project-lockup-title.t-black.ember-view > span.t-bold")
訣竅是找到一組唯一且靜態的元素屬性並將其作為目標。 對於該解決方案,我需要有關您的 HTML 的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.