我正在尝试使用硒锻炼,并且尝试了以下方法:
phone = driver.find_element_by_xpath(("//table[@class='contact_info_table']/tbody/tr/td[contains(text(), r'\+[0-9]*']"))
为了尝试获取表格内的电话号码。表格的HTML如下:
<table class="contact_info_table">
<tbody>
<tr>
<th>......</th>
<td>
. . .
</td>
</tr>
<tr>
<th>......</th>
<td>
. . .
</td>
</tr>
<tr>
<th>Telefono:</th>
<td><img style="vertical-align:middle; display: inline-block; margin-right: 10px;" src="../img/flags/it.png" title="Italia">+1234567890</td>
</tr>
我需要做的是在最后一个tr中获得1234567。我究竟做错了什么?
我得到的错误是:
InvalidSelectorException: Message: Given xpath expression "//table[@class='contact_info_table']/tbody/tr/td[contains(text(), r'\+[0-9]*']]" is invalid: SyntaxError: The expression is not a legal expression.
谢谢。
最佳答案
看起来您在路径的一部分中缺少括号-正则表达式后,您有两个关闭的]]
,但是您需要一个)
来替换倒数第二个]
才能关闭查询:
//table[@class='contact_info_table']/tbody/tr/td[contains(text(), r'\+[0-9]*')]
我也不确定您使用的正则表达式对于XPath是否有效。我认为您需要在XPath中使用
contains
方法-像这样:/td[matches(text(), '\+[0-9]*']]
我用
matches()
替换了contains
,并从正则表达式中删除了前面的matches
,因为我认为您的表达只需要是r
。希望这会有所帮助。您可能也根本不需要使用正则表达式来查找您要查找的元素。这是一个备用XPath,它不使用正则表达式,但仍通过获取所需的
'\+[0-9]*'
元素作为前面的td
的同级元素来定位该元素://th[text()='Telefono:']//following-sibling::td
因此,要获取电话号码,可以使用
th
:phone = driver.find_element_by_xpath("//th[text()='Telefono:']//following-sibling::td").text
这是外部div的HTML:
<div id="right">
<div class="profile-info">
<p class="l" id="sedcard-views" style="position: relative;">Visite Totali: 180 </p>
<p class="r">Ultima modifica: <span class="bold">06 Nov 2019</span></p>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="diamond_vip"></div>
<div class="profile-tabs">
<a href="javascript:void(0);" id="show_tabs" style="display:none">+</a>
<a href="javascript:void(0);" id="hide_tabs" style="">x</a>
<ul id="profile-tabs">
<li class="active" style="z-index: 5;">
<div class="l"></div>
<div class="m"><a href="#info">Bio</a></div>
<div class="r"></div>
</li>
<li class="" style="z-index: 4;">
<div class="l"></div>
<div class="m"><a href="#services">Servizi</a></div>
<div class="r"></div>
</li>
<li class="" style="z-index: 3;">
<div class="l"></div>
<div class="m"><a href="#price">Tariffe</a></div>
<div class="r"></div>
</li>
<li class="" style="z-index: 2;">
<div class="l"></div>
<div class="m"><a href="#contact">Contatti</a></div>
<div class="r"></div>
</li>
<li class="" style="z-index: 1;">
<div class="l"></div>
<div class="m"><a href="#comments">Rec. & Commenti</a></div>
<div class="r"></div>
</li>
</ul>
</div>
<div class="clear"></div>
<div id="profile-container" rel="6705" style="height: 3718px;">
<a rel="info"></a>
<div class="head info">
<span>NAME HERE</span>
<div class="clear"></div>
</div>
<div style="position: relative; min-height: 20px;">
<div class="absolute-right-box">
</div>
<table>
<tbody>
<tr>
<th>....</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>....:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
<tr>
<th>...:</th>
<td>...</td>
</tr>
</tbody>
</table>
</div>
<div class="clear"></div>
<table>
<tbody>
<tr>
<th>Lingue: </th>
<td>
<span class="strong">Inglese</span>
<span class="star-y"></span>
<span class="star-y"></span>
<span class="star-y"></span>
<span class="star-n"></span>
<div class="clear"></div>
<span class="strong">Russo</span>
<span class="star-y"></span>
<span class="star-y"></span>
<span class="star-y"></span>
<span class="star-y"></span>
<div class="clear"></div>
</td>
</tr>
<tr>
<th>....:</th>
<td>
<span class="incall">
..... </span>
<span class="outcall">
..... </span>
</td>
</tr>
<tr>
<th>....:</th>
<td>
.... <img src="https://.....gif" width="12" height="12" alt="...">
</td>
</tr>
<tr>
<th>...:</th>
<td>
.... </td>
</tr>
</tbody>
</table>
<div class="clear"></div>
<div class="about-block">
<a rel="about"></a>
<div class="head">...:</div>
<div class="about-text">
<p>...i</p>
</div>
</div>
<a rel="services"></a>
<div class="block services">
<div class="head"><span>...</span></div>
<ul>
<li>
...
</li>
<li>
...
<span class="pink">....</span>
</li>
<li>
...
<span class="pink">...</span>
</li>
<li>
...
<span class="pink">...</span>
</li>
<li>
...
</li>
<li>
....
</li>
</ul>
<ul>
<li>
...
</li>
<li>
...
</li>
<li>
...
</li>
<li>
...
</li>
<li>
...
</li>
<li>
...
</li>
</ul>
<div class="clear"></div>
</div>
<div class="clear"></div>
<a rel="price"></a>
<div class="block rates">
<div class="head"><span>...</span></div>
<table class="incall">
<tbody>
<tr>
<td colspan="2"><span class="icon">...</span></td>
</tr>
<tr>
<th>
...:</th>
<td>...</td>
</tr>
<tr>
<th>
...:</th>
<td>...</td>
</tr>
<tr>
<th>
...:</th>
<td>...</td>
</tr>
</tbody>
</table>
<table class="outcall">
<tbody>
<tr>
<td colspan="2"><span class="icon">...</span></td>
</tr>
<tr>
<th>
...:</th>
<td>...</td>
</tr>
<tr>
<th>
...:</th>
<td>...</td>
</tr>
</tbody>
</table>
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="clear"></div>
<a rel="tours"></a>
<a rel="contact"></a>
<div class="new_contacts">
<ul id="contactTabs">
<li class="active">
<div class="l"></div>
<div class="m"><a id="contactTab" class="active contact" href="javascript:void(0);"><span>Contatti</span></a></div>
<div class="r"></div>
</li>
</ul>
<div class="n_cont">
<div class="contactTab block contact">
<div class="contact_info">
<table class="contact_info_table">
<tbody>
<tr>
<th>Città base:</th>
<td>
00192 <a href="..." class="bold" title="...">
... </a>
</td>
</tr>
<tr>
<th>Zone città:</th>
<td>
<a href="....">...</a> </td>
</tr>
<tr>
<th>Telefono:</th>
<td><img style="vertical-align:middle; display: inline-block; margin-right: 10px;" src="../img/flags/pt.png" title="...">+1234567</td>
</tr>
<tr>
<th>Apps Available: </th>
<td>
<div class="whatsapp">WhatsApp</div>
<div class="clear"></div>
</td>
</tr>
<tr>
<th>....: </th>
<td class="bold">...</td>
</tr>
<tr>
<th>...:</th>
<td class="bold">
<div style="display: block; float: left">
<a style="display: block; width: 90px; height: 18px; outline: none; margin-top: 2px;" href="" onclick="return Cubix.PrivateMessaging.Show($(this).getParent(), '...')">
<img src="https://.....png" alt="Contattami">
</a>
</div>
</td>
</tr>
</tbody>
</table>
<div class="ext-site ">
<table>
<tbody>
<tr>
<th>
<span><img src="...png"></span>
</th>
<td>
<a href="http:/..." target="_blank">....!</a>
</td>
</tr>
</tbody>
</table>
</div>
<div class="clear"></div>
<div class="borderElm"></div>
<table class="working-times">
<thead>
<tr>
<td colspan="2">...</td>
</tr>
</thead>
<tbody>
<tr>
<td align="center">
<p class="available_24_7">....</p>
</td>
</tr>
</tbody>
</table>
<div class="borderElm"></div>
<div class="clear"></div>
</div>
</div>
<div class="mapTab none">
<div id="map_canvas" style="width:100%; height:370px"></div>
</div>
<div class="clear"></div>
</div>
</div>
<a rel="comments"></a>
<div class="reviews_comments">
<ul id="rcTabs">
<li class="active">
<div class="l"></div>
<div class="m"><a id="cTab" class="comments active" href="javascript:void(0);"><span>Commenti<i> (0)</i></span></a></div>
<div class="r"></div>
</li>
<li class="">
<div class="l"></div>
<div class="m"><a id="rTab" class="reviews " href="javascript:void(0);"><span>Recensioni (0)</span></a></div>
<div class="r"></div>
</li>
</ul>
<div class="r_c">
<div class="cTab block" style="">
<div class="head comments p0"><span><span class="title"></span> <span class="btn-blue-slim add-comment">Scrivi commenti</span></span>
</div>
<div id="commentsBody" class="ajax">
<div id="add-comment-form"></div>
<p class="comment_added" id="comment_added">Comment is awaiting moderation</p>
<div id="comments_container" style="position: relative; opacity: 1;">
<p class="no_comment">Non esiste alcun commento</p>
<input type="hidden" id="commentCount" value="0">
</div>
</div>
<div class="clear"></div>
</div>
<div class="rTab reviews none">
<div class="reviews-body">
<a class="btn-blue-slim" href="...">Aggiungi/Vedi recensioni</a>
<div class="clear"></div>
</div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="margin1000"> </div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
关于python - python selenium说xpath表达式无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58868212/