I'm scrapping facebook post, I'm using
status = x.find_element_by_xpath(".//div[@class='_5pbx userContent _3576']/p[1]").text
for getting status post, and html structure of facebook post like here
<div class="_5pbx userContent _3576" data-ft="{{"tn":"K"}}" id="js_33">
<p>Bersama Ketua APTIKOM Yogyakarta, Bapak Dr. Agfianto Eko Putra, M.Si. @agfi68<br> Terima kasih banyak telah memesan buku Digital.Is. Me</p>
</div>
sometimes, p tag inside div, but sometime not inside div
how can I create status = x.find_element_by_xpath(".//div[@class='_5pbx userContent _3576']/p[1]").text
that support if inside div or not inside div
您可以执行x.find_elements_by_xpath
并查看长度是否为零。
Use find_element_by_xpath(".//div[@class='_5pbx userContent _3576']")
to find the <div>
element.
Then you can inspect that element to see whether or not it has a <p>
child element.
You want to select two different elements depending on certain conditions. So, you should use:
.//div[@class='_5pbx userContent _3576'][not(p[1])]|.//div[@class='_5pbx userContent _3576']/p[1]
In general, assuming variables $XPathExpression1
, $XPathExpression2
and $Condition
:
$XPathExpression1[$Condition]|$XPathExpression2[not($Condition)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.