简体   繁体   English

Xpath 选择器 - 是否可以在 selenium 中按标题查找元素?

[英]Xpath Selectors - is it possible to find elements by title in selenium?

I've been trying to build a bot that scrapes the name of followers liking a picture in instagram.我一直在尝试构建一个机器人,它可以在 Instagram 上抓取喜欢图片的追随者的名字。 The website opens a popup box with the accounts in side, and the box seemingly refreshes the account links as you scroll it.该网站会打开一个弹出框,其中包含帐户,并且该框似乎会在您滚动时刷新帐户链接。 I've written a code that will open the box and then scroll it, but i can't get selenium to scrape the account names.我编写了一个代码,可以打开该框然后滚动它,但我无法让 selenium 刮取帐户名称。 My code looks like this to scroll the pop up box:我的代码看起来像这样滚动弹出框:

realscroll_box = browser.find_element_by_xpath('/html/body/div[4]/div/div[3]/div')
while last_ht != ht:
last_ht = ht
time.sleep(2)
ht = browser.execute_script('''
                            arguments[0].scrollTo(0, arguments[0].scrollHeight);
                            return arguments[0].scrollHeight;
                            ''', realscroll_box)
namelinkstemp1 = realscroll_box.find_elements_by_xpath('//*[contains(@href,"/")')

But it gives me this error:但它给了我这个错误:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression // [starrts-with(@href,"/") because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '// [starrts-with(@href,"/")' is not a valid XPath expression. selenium.common.exceptions.InvalidSelectorException:消息:无效选择器:无法找到具有 xpath 表达式的元素 // [starrts-with(@href,"/") 因为以下错误:SyntaxError:无法执行“评估”在“文档”上:字符串“// [starrts-with(@href,"/")”不是有效的 XPath 表达式。

The text i'm trying to extract looks like this:我试图提取的文本如下所示:

<a title="instagramusername" href="/instagramusername/">
<div class="                   Igw0E     IwRSH      eGOV_     ybXk5    _4EzTm                                                                                                              ">
<div class="_7UhW9   xLCgt       qyrsm KV-D4            fDxYl      rWtOq">
<div class="                   Igw0E   rBNOH        eGOV_     ybXk5    _4EzTm                                                                                                              ">instagramusername</div>
</div>
</div>
</a>

Any help is appreciated, xpath queries are very new to me.感谢您提供任何帮助,xpath 查询对我来说非常新。

Thanks:)谢谢:)

Starts-with function finds the element based on attribute value. Starts-with function 根据属性值查找元素。 In your code function is not correct.在您的代码中 function 不正确。 Also //*[contains(@href,"/") , here you missed ] Correct one: //a[contains(@href, '/')]还有//*[contains(@href,"/") ,这里你错过了]更正一个: //a[contains(@href, '/')]

  //*[starts-with(@href,'/')]

or或者

//a[starts-with(@href,'/instagramuse')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM