向下滚动表selenium python

时间:2018-05-28 14:59:25

标签: python selenium bots

我正在尝试编写一个程序,用于向下滚动到html ul中的特定项目。 我试着用

driver.execute_script("window.scrollBy(0,2000)")

但它没有做任何事情。

我也尝试了以下内容:

elem = driver.find_element_by_css_selector("._p4iax")
segui = driver.find_element_by_css_selector ("._qv64e._gexxb._4tgw8._njrw0")
elem.execute_script("arguments[0].scrollIntoView(true);", segui)

这给了我这个错误:

AttributeError: 'WebElement' object has no attribute 'execute_script'

我正在尝试向下滚动的元素是:

enter image description here

我尝试定位的所有元素都有相同的错误。我做错了什么?

这是关于chrome的关注者的Instagram表

<div><div class="_pfyik" role="dialog"><div class="_23gmb"></div><div class="_o0j5z"><div class="_784q7 _3g81g"><div class="_lfwfo _euzqy">Follower</div><div class="_gs38e"><ul class="_8q670 _b9n99"><div class="_p4iax"><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/officialj0ny___/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/9ab86389e5df1c7a37574e022ba14ed1/5B85E7B5/t51.2885-19/s150x150/32786838_1846073385693465_2650300928246153216_n.jpg" alt="Immagine del profilo di officialj0ny___"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="officialj0ny___" href="/officialj0ny___/">officialj0ny___</a></div><div class="_9mmn5 ">Jny_Hussein</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/stefanopalmieri33/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/b1efae9125caf9e6fb987b8b93dd2f97/5B85E091/t51.2885-19/s150x150/32872493_1386146181518292_488509455692988416_n.jpg" alt="Immagine del profilo di stefanopalmieri33"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="stefanopalmieri33" href="/stefanopalmieri33/">stefanopalmieri33</a></div><div class="_9mmn5 ">Stefano❤</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e       _gexxb _4tgw8      _njrw0   ">Segui</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/hypewearesell/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/557be8c71f51e41dc0c42457595b1676/5BC18C1A/t51.2885-19/s150x150/28753325_436023380163255_3484110133741486080_n.jpg" alt="Immagine del profilo di hypewearesell"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="hypewearesell" href="/hypewearesell/">hypewearesell</a></div><div class="_9mmn5 ">HypeWeaResell</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/boumybabyfootwear/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/afd7774ad3d4c5a9b2bb3a5be741ab6b/5BA1750F/t51.2885-19/s150x150/21297031_1888788934781457_1877007236425842688_a.jpg" alt="Immagine del profilo di boumybabyfootwear"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="boumybabyfootwear" href="/boumybabyfootwear/">boumybabyfootwear</a></div><div class="_9mmn5 ">Boumy</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/enrico_lavaroni/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/8e2dad02718e1eb9300aaf1dd6f88050/5BA9CD4E/t51.2885-19/s150x150/29095705_1504363363023220_6280469469361864704_n.jpg" alt="Immagine del profilo di enrico_lavaroni"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="enrico_lavaroni" href="/enrico_lavaroni/">enrico_lavaroni</a></div><div class="_9mmn5 ">Enrico Lavaroni</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/theov87/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/a1ad859e78e3762671e8452634182270/5B861D8C/t51.2885-19/s150x150/28752854_1992368050792927_4580622186422730752_n.jpg" alt="Immagine del profilo di theov87"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="theov87" href="/theov87/">theov87</a></div><div class="_9mmn5 ">Theo V</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/vctorfdez/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/45c24ce7d5d746b19c77e7ab33bd1dfb/5B85E58C/t51.2885-19/s150x150/32896368_1984686215181161_7022291479083089920_n.jpg" alt="Immagine del profilo di vctorfdez"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="vctorfdez" href="/vctorfdez/">vctorfdez</a></div><div class="_9mmn5 ">'' VIC '' </div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/michele.m.cristini/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/e3eb4324e83b39374636b69f799ac21e/5B84224C/t51.2885-19/s150x150/32006559_2029887197262837_6259419546881359872_n.jpg" alt="Immagine del profilo di michele.m.cristini"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="michele.m.cristini" href="/michele.m.cristini/">michele.m.cristini</a></div><div class="_9mmn5 ">Michele Cristini</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/jaco_sanson/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/6004b16e65498cca3717256ed6132b56/5BBB3F39/t51.2885-19/s150x150/30829557_151235709053706_8368199195877703680_n.jpg" alt="Immagine del profilo di jaco_sanson"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="jaco_sanson" href="/jaco_sanson/">jaco_sanson</a></div><div class="_9mmn5 ">jacopo sanson</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/chasingsuccesstwin/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/cb9501d338d5888f56a1ec1be18bbee7/5BA5D2FF/t51.2885-19/s150x150/30603809_1211476955655505_9195069044187725824_n.jpg" alt="Immagine del profilo di chasingsuccesstwin"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="chasingsuccesstwin" href="/chasingsuccesstwin/">chasingsuccesstwin</a></div><div class="_9mmn5 ">Twin</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/thy.dunhill/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/dafa8871aa72ab0834c8ae83bc04383d/5B8A09C4/t51.2885-19/s150x150/30593579_462494067502571_3151334482804473856_n.jpg" alt="Immagine del profilo di thy.dunhill"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="thy.dunhill" href="/thy.dunhill/">thy.dunhill</a></div><div class="_9mmn5 ">Jeffrëy Dûnhîll</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li><li class="_6e4x5"><div class="_npuc5"><div class="_f5wpw"><a class="_9irns _pg23k _jpwof _gvoze" href="/_chico_alex_/" style="width: 30px; height: 30px;"><img class="_rewi8" src="https://scontent-mxp1-1.cdninstagram.com/vp/3572390f74d2237c04e956cbaa663bff/5B89C79C/t51.2885-19/s150x150/27576758_399957540450284_3985554063835004928_n.jpg" alt="Immagine del profilo di _chico_alex_"></a><div class="_eryrc"><div class="_2nunc"><a class="_2g7d5 notranslate _o5iw8 " title="_chico_alex_" href="/_chico_alex_/">_chico_alex_</a></div><div class="_9mmn5 ">Alex 2003</div></div></div><div class="_mtnzs"><span class="_ov9ai"><button class="_qv64e    _t78yp    _4tgw8      _njrw0   ">Segui già</button></span></div></div></li></div></ul><div class="_gwyj6 _jfct1"><div class="_4c5eh"><iframe title="Intentionally left blank" aria-hidden="true" class="_o6ctz" tabindex="-1"></iframe></div></div></div></div></div><button class="_dcj9f">Chiudi</button></div></div>

2 个答案:

答案 0 :(得分:0)

我认为您的错误可能是因为elem是驱动程序找到的WebElement,而您正在使用带有elem的execute_script,而不是驱动程序。您是否可以尝试使用findElements作为列表元素并可能迭代找到正确的元素? 我希望这有一些帮助。

答案 1 :(得分:0)

将以下属性用作CSS选择器。

li._6e4x5:nth-child(5)