我試圖從 Python Selenium 中的 div 類中提取一個特定的數字,但不知道該怎么做。我想獲得“post_parent” ID 947630,只要它與“post_name”號碼開始匹配09007。
我希望在多個“post_name”類中執行此操作,所以我會提供這樣的內容:search_text = "0900766b80090cb6",但將來會有多個,所以它必須先讀取“post_name”,然后拉“post_parent”如果這就說得通了。
感謝任何人提供的任何建議。
<div class="hidden" id="inline_947631">
<div class="post_title">Interface Converter</div>
<div class="post_name">0900766b80090cb6</div>
<div class="post_author">28</div>
<div class="comment_status">closed</div>
<div class="ping_status">closed</div>
<div class="_status">inherit</div>
<div class="jj">06</div>
<div class="mm">07</div>
<div class="aa">2001</div>
<div class="hh">15</div>
<div class="mn">44</div>
<div class="ss">17</div>
<div class="post_password"></div>
<div class="post_parent">947630</div>
<div class="page_template">default</div>
<div class="tags_input" id="rs-language-code_947631">de</div>
</div>
uj5u.com熱心網友回復:
如果你看到<div >0900766b80090cb6</div>這個并且<div >947630</div>是彼此的兄弟節點。
你可以xpath -> following-sibling這樣使用:
代碼:
search_text = "0900766b80090cb6"
post_parent_num = driver.find_element(By.XPATH, f"//div[@class='post_name' and text()='{search_text}']//following-sibling::div[@class='post_parent']").text
print(post_parent_num)
或使用 ExplicitWait:
search_text = "0900766b80090cb6"
post_parent_num = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, f"//div[@class='post_name' and text()='{search_text}']//following-sibling::div[@class='post_parent']"))).get_attribute('innerText')
print(post_parent_num)
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新:
NoSuchElementException:
如果我們有獨特的條目,請檢查dev tools(谷歌瀏覽器)。HTML-DOM
你應該檢查的xpath:
//div[@class='post_name' and text()='0900766b80090cb6']//following-sibling::div[@class='post_parent']
檢查步驟:
Press F12 in Chrome-> 轉到element部分 -> 執行CTRL F-> 然后粘貼xpath并查看,如果您想要element的是否使用匹配節點突出顯示。1/1
如果這是唯一的//div[@class='post_name' and text()='0900766b80090cb6']//following-sibling::div[@class='post_parent'],那么您還需要檢查以下條件。
檢查它是否在任何
iframe/frame/frameset.解決方法:先切換到iframe/frame/frameset,再與這個web元素互動。
檢查它是否在任何
shadow-root.解決方法:使用
driver.execute_script('return document.querySelector回傳一個web元素,然后進行相應操作。確保在與元素互動之前正確呈現元素。放一些
hardcoded delay或Explicit wait再試一次。解決方案:
time.sleep(5)或WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='post_name' and text()='0900766b80090cb6']//following-sibling::div[@class='post_parent']"))).textIf you have redirected to a
new tab/ or new windowsand you have not switched to that particularnew tab/new window, otherwise you will likely getNoSuchElementexception.Solution: switch to the relevant window/tab first.
If you have switched to an iframe and the new desired element is not in the same iframe context then first
switch to default contentand then interact with it.Solution: switch to default content and then switch to respective iframe.
uj5u.com熱心網友回復:
您可以創建一個方法并使用以下方法根據文本xpath獲取文本。post_parentpost_name
def getPostPatent(postname):
element=driver.find_element(By.XPATH,"//div[@class='post_name' and starts-with(text(),'{}')]/following-sibling::div[@class='post_parent']".format(postname))
print(element.get_attribute("textContent"))
getPostPatent('09007')
如果它與文本匹配,這將回傳值starts-with('09007')
似乎父類是隱藏的,您需要使用它textContent來獲取值。
uj5u.com熱心網友回復:
我沒有看到 "post_parent" ID947630和 "post_name" 號碼開始之間有任何特定關系09007。此外,父母<div>正在擁有.
但是,要提取特定數字,您可以使用以下任一定位器策略:
使用css_selector:
print(driver.find_element(By.CSS_SELECTOR, "div[id^='inline'] div.post_parent").text)使用xpath:
print(driver.find_element(By.XPATH, "//div[starts-with(@id, 'inline_')]//div[@class='post_parent']").text)
理想情況下,您需要為presence_of_element_located()引入WebDriverWait ,并且可以使用以下任一定位器策略:
使用CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div[id^='inline'] div.post_parent"))).text)使用XPATH:
print(WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, "//div[starts-with(@id, 'inline_')]//div[@class='post_parent']"))).text)注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/451809.html
