未能在硒中找到元素 python

Failed to find an element in selenium python

我在 python2 中使用硒。我想使用 (driver.find_elements_by_class_name()) 找到一个元素 但是,该代码没有结果。但是,如果我尝试使用 (driver.find_element_by_class_name()) 查找元素,我会收到错误消息。

这是我的一些代码:

def test():
    driver.get('https://mbasic.facebook.com/aldhy.tompo')
    button_lainnya = driver.find_element_by_link_text('Lainnya')
    Class = button_lainnya.get_attribute('class')
    print Class
    all_button = driver.find_elements_by_class_name(Class)
    print all_button

test()

输出为:

cr cs
[]

如果我修改第 6 行的代码为

all_button = driver.find_element_by_class_name(Class)

我收到错误消息:

Traceback (most recent call last):
  File "c:/Users/LENOVO/OneDrive/Documents/project/python/Selenium/robot olshop.py", line 119, in <module>
    test()
  File "c:/Users/LENOVO/OneDrive/Documents/project/python/Selenium/robot olshop.py", line 114, in test
    pesan = driver.find_element_by_class_name(element)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .cr cs

这是网站的来源:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>ALdhy</title><meta name="referrer" content="origin-when-crossorigin" id="meta_referrer" /><style type="text/css">/*<![CDATA[*/.cz{background:#fff;}.db{background:#e9ebee;}.dj{position:relative;top:1px;}.b .dc{padding:0;}.b .w{padding:2px;}.b .dg{padding:4px;}.dz{color:#4b4f56;}.dv{color:#1d2129;}.du{font-size:14px;line-height:20px;}.dk{font-size:16px;line-height:20px;}.ba{font-weight:normal;}.da{font-weight:bold;}.b .df{border-collapse:collapse;margin:0;width:100%;}.b td.di{border:0;padding:6px 0 6px 6px;vertical-align:middle;}.b .df .dl{padding:6px 6px 6px 0;text-align:right;white-space:nowrap;}.b .dh{padding:6px;}.b .m{border:0;border-collapse:collapse;margin:0;padding:0;width:100%;}.b .m tbody{vertical-align:top;}.b .em>tr>td,.b .em>tbody>tr>td,.b .m td.em{vertical-align:middle;}.b .m td{padding:0;}.b .m td.w{padding:2px;}.b .m td.dg{padding:4px;}.b .t{width:100%;}.b .dp{padding:4px;}.dm>*,.dm.dm>*{border-bottom:1px solid #e5e5e5;}.dm>:last-child{border-bottom:none;}.dm+.dm{border-top:1px solid #e5e5e5;}.ec{float:right;max-width:85%;overflow:hidden;text-align:right;text-overflow:ellipsis;}.ea{-moz-user-select:none;}.dy{width:100%;}.dy .eb{text-align:right;}.bj .bl .ce{color:#42464d;font-size:19px;font-weight:bold;}.bj.b .bl .ce{margin-left:0;position:static;}.bj.b .bl .cg{margin-left:5px;vertical-align:baseline;}.bj .bl .ct{padding-left:4px;}.ed{position:relative;}.b .bs{height:100%;overflow:hidden;position:absolute;}.b .bm{background-color:#1c1e21;height:100px;position:relative;}.b .bt{height:auto;}.b .bu{width:inherit;}.b .br{width:100%;}.b .bw{margin-top:-45px;}.cv{word-wrap:break-word;}.ca{position:relative;}.cb{border:2px solid #fff;box-shadow:0 1px 2px rgba(0, 0, 0, .1);display:inline-block;}.b .cd{padding:5px 0 5px 0;text-align:center;}.b .bz{text-align:center;}.bx{background-color:#fff;}.i{background-color:#3b5998;}.cx{background-color:#d8dfea;border-top:solid 1px #3b5998;}.j{padding:2px 3px;}.ee{padding:4px 3px;}.by{padding:6px 3px;}.b a,.b a:visited{color:#3b5998;text-decoration:none;}.b .cu,.b .cu:visited{color:#6d84b4;}.b .bg,.b .bg:visited{color:#fff;}.b a:focus,.b a:hover,.b .cu:focus,.b .cu:hover{background-color:#3b5998;color:#fff;}.b .bg:focus,.b .bg:hover{background-color:#fff;color:#3b5998;}.ci{color:gray;}.ch{font-size:small;}body,tr,input,textarea,.f{font-size:medium;}.dt{padding-left:4px;}.cj .cl{display:inline-block;margin-right:4px;}.b .cj .cl{float:left;}.b .cj .cm{display:table-cell;}.s{border:0;display:inline-block;vertical-align:top;}i.s u{position:absolute;width:0;height:0;overflow:hidden;}.dw{margin-right:4px;}.co{clear:both;}.dr{border-radius:50%;}.cc{background:#eceff5;}.cf{margin:2px 0 0 5px;}.cq .cr{display:block;}.cr{border:solid 2px;cursor:pointer;margin:0;padding:2px 6px 3px;text-align:center;}.cs,.b a.cs{background:#f3f4f5;border-color:#ccc #aaa #999;color:#505c77;}.i .cs,.b .i a.cs{background:#3b5998;border-color:#8a9ac5 #29447E #1a356e;color:#fff;}.cr .s{pointer-events:none;}.cr{display:inline-block;}.cr+.cr{margin-left:3px;}.cr input{background:none;border:none;margin:0;padding:0;}.cs input{color:#505c77;}.i .cs input{color:#fff;}.bv{background:#f2f2f2;}.bp:hover .bv{background:none;}body{text-align:left;direction:ltr;}body,tr,input,textarea,button{font-family:sans-serif;}body,p,figure,h1,h2,h3,h4,h5,h6,ul,ol,li,dl,dd,dt{margin:0;padding:0;}h1,h2,h3,h4,h5,h6{font-size:1em;font-weight:bold;}ul,ol{list-style:none;}article,aside,figcaption,figure,footer,header,nav,section{display:block;}.e #viewport{margin:0 auto;max-width:600px;}#page{position:relative;}.r,.r.s{display:block;}.o{display:block;}.p{height:20px;width:20px;}.k{background:#3b5998;padding:0 4px 4px;height:22px;}.k.k .x{background:#fff;border:1px solid #243872;box-sizing:border-box;font-size:small;height:22px;margin:0;width:100%;}.l.k{padding:1px 1px 3px;}.k .q{padding:1px 3px 0 0;}.k.k.k .bc{background:#627aba;border:1px solid #2e417e;color:#fff;font-size:x-small;font-weight:normal;height:22px;line-height:20px;margin-left:3px;}form{margin:0;border:0;}.v{border:0;display:block;margin:0;padding:0;}.bb{-moz-appearance:none;background:none;display:inline-block;font-size:12px;height:28px;line-height:28px;margin:0;overflow:visible;padding:0 9px;text-align:center;vertical-align:top;white-space:nowrap;}.b .bb{border-radius:2px;}.bd,a.bd,html .b a.bd{color:#fff;}.b .bd{background-color:#4267b2;border:1px solid #365899;}.b a.bd:hover,.b .bd:hover{background-color:#465e91;}.bd[disabled]{color:#899bc1;}.b .bd[disabled]:hover{background-color:#4267b2;}.b a.bb::after{content:"";display:inline-block;height:100%;vertical-align:middle;}.b .bb{padding:0 8px;}.b a.bb{height:26px;line-height:26px;}.ef{font-size:small;padding:7px 8px 8px;}.ew{border:1px solid;border-color:#8d949e;border-radius:4px;display:block;margin-top:8px;padding:4px;text-align:center;}.eu{display:block;font-size:x-small;margin:-3px -3px 1px -3px;padding:3px;}.b .ef td.et{padding-right:4px;}.b .ef td.ev{padding-left:4px;}.ef.eg{background-color:#444950;}.eg{border-top:1px solid #444950;color:#bec3c9;}.b .eg a,.b .eg a:visited{color:#bec3c9;}.b .eg a:focus,.b .eg a:hover{background:#dadde1;color:#1d2129;}.ei{margin-bottom:8px;}.ef.eg .ei>table{background:#d3d7dc;border:1px solid #444950;}.ep{background:#d3d7dc;}.eh .es{height:24px;line-height:24px;margin-left:2px;}.ek{background:#fff;}.eh .eq{background-color:transparent;color:#4b4f56;display:block;padding:0;width:100%;}.el .s{display:block;}.eh .ej .er{padding:2px;}.eh .ej .el{padding:4px;}.b .eh .ej{border:1px solid #8d949e;}.be{padding-bottom:1px;}.bf{display:inline-block;font-size:small;padding:2px 4px 2px;}.bh{color:#fff496;}.b a:hover .bh,.b a:focus .bh,.bf:hover .bh,.bf:focus .bh{color:#365899;}/*]]>*/</style><link rel="manifest" href="/data/manifest/" crossorigin="use-credentials" /></head><body tabindex="0" class="b c d e"><div class="f"><div id="viewport"><div class="g h" id="MChromeHeader"><div class="i j" role="banner" id="header"><form method="get" action="/search/" class="k l"><input type="hidden" name="search" value="Search" /><input type="hidden" name="search_source" value="top_nav" /><table class="m" role="presentation"><tbody><tr><td class="n"><a class="o p q" href="/home.php?ref_component=mbasic_home_logo&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo"><img src="https://static.xx.fbcdn.net/rsrc.php/v3/ym/r/Gjhrhb7r0lb.png" width="20" height="20" class="r s" alt="Facebook logo" /></a></td><td class="t u"><input class="v w x" name="query" placeholder="Cari Facebook" autocomplete="off" autocorrect="off" spellcheck="false" type="text" /></td><td class="n y"><input value="Cari" type="submit" class="z ba bb bc bd" /></td></tr></tbody></table></form><div role="navigation" class="be"><a href="/home.php?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg">Beranda</a><a href="/fhathonnach.shello?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg">Profil</a><a accesskey="4" href="/messages/?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg"><strong class="bh">Pesan<span aria-label="6 belum dibaca">(6)</span></strong></a><a accesskey="3" href="/notifications.php?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg"><strong class="bh">Notifikasi<span aria-label="4 baru">(4)</span></strong></a><a accesskey="6" href="/buddylist.php?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg">Obrolan<span aria-label="194 tersedia">(194)</span></a><a accesskey="2" href="/friends/center/mbasic/?fb_ref=tn&amp;sr=1&amp;ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg">Teman<span aria-label="971 permintaan baru">(971)</span></a><a href="/pages/?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg"><strong class="bh">Halaman<span aria-label="8 pemberitahuan baru">(8)</span></strong></a><a href="/groups/?category=membership&amp;ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg"><strong class="bh">Grup(39)</strong></a><div class="bf" style="display:block;height:0;overflow:hidden;position:absolute;width:0;padding:0"><a href="/groups/?category=membership&amp;a_type=sr" aria-label="39 postingan grup belum dilihat" tabindex="-1">&nbsp;</a><a href="/events/?a_type=sr" aria-label="Acara" tabindex="-1">&nbsp;</a><a href="/notes/?a_type=sr" aria-label="Catatan" tabindex="-1">&nbsp;</a><a href="/saved/?a_type=sr" aria-label="Menyimpan 1 baru" tabindex="-1">&nbsp;</a><a href="/settings/?a_type=sr" aria-label="Pengaturan &amp; Privasi" tabindex="-1">&nbsp;</a><a href="/help/?a_type=sr" aria-label="Bantuan" tabindex="-1">&nbsp;</a></div><a accesskey="5" href="/menu/bookmarks/?ref_component=mbasic_home_header&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" class="bf bg">Menu</a></div></div></div><div id="objects_container"><div class="bi" id="root" role="main"><div class="bj bk"><div class="bl"><div><div class="bm bn"><div class="bo" id="profile_cover_photo_container"><a href="/photo.php?fbid=327420491088444&amp;id=100014614866494&amp;set=a.238816733282154&amp;source=44" class="bp bq br bs" title="Foto Sampul: Foto ALdhy."><img src="https://scontent.fcgk24-1.fna.fbcdn.net/v/t1.0-9/cp0/e15/q65/c0.423.480.160a/26815493_327420491088444_1281706991021315294_n.jpg?_nc_cat=105&amp;efg=eyJpIjoiYiJ9&amp;_nc_oc=AQkhUONjtbuCFItR6Lk3h_7hcd--7T1jMINkeYWyvzWLIST129G7yx5jfheQSmUWgHI&amp;_nc_ht=scontent.fcgk24-1.fna&amp;oh=429c5f77922a1e93371c495cf5f1c50c&amp;oe=5E5580F2" width="320" height="160" class="bt bu bv s" /></a></div></div></div><div class="bw bx by"><div><div class="bz"><div class="ca cb"><a href="/photo.php?fbid=697134654117024&amp;id=100014614866494&amp;set=a.107340373096458" id="u_0_2"><img src="https://scontent.fcgk24-1.fna.fbcdn.net/v/t1.0-1/cp0/e15/q65/c12.0.74.74a/p74x74/75398266_697134660783690_3381090451179175936_o.jpg?_nc_cat=108&amp;efg=eyJpIjoiYiJ9&amp;_nc_oc=AQld6oI7W4qR3K81zygg6KwiL5nJZ6y9jfi6rXSIsxp7HS88TiAUeSdI74KcjIKilhw&amp;_nc_ht=scontent.fcgk24-1.fna&amp;oh=386b6fde17bfcd7fbaaf5308b54e7f89&amp;oe=5E4AF13B" class="cc s" alt="ALdhy" /></a></div></div><span><div class="cd"><span><strong class="ce">ALdhy <span class="alternate_name">(Tompo)</span></strong><img src="https://static.xx.fbcdn.net/rsrc.php/v3/yK/r/Ye1TQi63ARL.png" width="8" height="12" class="cf cg s" aria-label="ALdhy dapat dihubungi melalui teleponnya" /></span></div><span class="ch ci"><span><div class="cj ck" role="heading" id="u_0_1"><img src="https://static.xx.fbcdn.net/rsrc.php/v3/yS/r/1HRQT0OcHKI.png" width="12" height="14" class="cl s" role="presentation" aria-level="4" /><span class="cm ch">Bekerja di <a class="cn" href="/profile.php?id=107980922563405">Swasta</a></span><div class="co"></div></div></span></span></span></div></div><div class="cp"><table class="cq"><tr><td><a href="/a/mobile/friends/profile_add_friend.php?subjectid=100014614866494&amp;istimeline=1&amp;hf=profile_button&amp;fref=gm&amp;frefid=0&amp;referrer_uri=https%3A%2F%2Fmbasic.facebook.com%2Fbrowse%2Fgroup%2Fmembers%2F%3Fid%3D1536129013327270%26start%3D0%26listType%3Dlist_nonfriend_nonadmin%26refid%3D18%26ref%3Ddbl&amp;gfid=AQBcZDwrl6y7AaxA" class="cr cs">Tambah Jadi Teman</a></td><td><a href="/messages/thread/100014614866494/?entrypoint=profile_message_button" class="cr cs">Pesan</a></td><td><a href="/mbasic/more/?owner_id=100014614866494" class="cr cs">Lainnya</a></td></tr></table></div><div class="ct f ci"><a href="/aldhy.tompo?v=timeline&amp;lst=100003433068177%3A100014614866494%3A1573644042" class="cu">Linimasa</a> · <a href="/aldhy.tompo/friends?lst=100003433068177%3A100014614866494%3A1573644042" class="cu">Teman</a> · <a href="/aldhy.tompo/photos?lst=100003433068177%3A100014614866494%3A1573644042" class="cu">Foto</a> · <a href="/aldhy.tompo?v=likes&amp;lst=100003433068177%3A100014614866494%3A1573644042" class="cu">Suka</a></div></div><div class="cv"><div class="cw bx"><h3 class="cx j">Teman</h3></div><div><div class="bx"><a href="/aldhy.tompo/friends?lst=100003433068177%3A100014614866494%3A1573644042">Lihat Semua Teman (1040)</a></div></div></div><div class="cy"><div id="work"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Pekerjaan</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="do dp" id="u_0_0"><div class="cj dq"><a class="cl" href="/profile.php?id=107980922563405"><img src="https://scontent.fcgk24-1.fna.fbcdn.net/v/t1.0-1/cp0/e15/q65/c14.0.48.48a/p48x48/399548_10149999285987789_1102888142_n.png.jpg?_nc_cat=1&amp;efg=eyJpIjoiYiJ9&amp;_nc_oc=AQl8gYxpUBwBm01VIUkCjywHWgrHloJpYkVJY8bXRaDXHJWwKfRiC-GVvrpLBOvO3tU&amp;_nc_ht=scontent.fcgk24-1.fna&amp;oh=d390e65f185dc2fae2c2bbec1e845648&amp;oe=5E60DEE1" class="dr ds cc s" alt="Swasta" /></a><div class="dt cm"><div><span class="du dv da dw"><a class="cn" href="/profile.php?id=107980922563405">Swasta</a></span></div></div><div class="co"></div></div></div></div></div></div></div><div id="contact-info"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Info Kontak</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="do dp dx" title="Facebook"><table cellspacing="0" cellpadding="0" class="dy"><tr><td valign="top"><div class="dz ea"><span class="dk dz ba">Facebook</span></div></td><td valign="top" class="eb"><div class="ec"> /aldhy.tompo</div></td></tr></table></div></div></div></div><div id="basic-info"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Info Umum</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="do dp dx" title="Jenis Kelamin"><table cellspacing="0" cellpadding="0" class="dy"><tr><td valign="top"><div class="dz ea"><span class="dk dz ba">Jenis Kelamin</span></div></td><td valign="top" class="eb"><div class="ec">Laki-laki</div></td></tr></table></div></div></div></div><div id="nicknames"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Nama Lain</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="do dp dx" title="Nama panggilan"><table cellspacing="0" cellpadding="0" class="dy"><tr><td valign="top"><div class="dz ea"><span class="dk dz ba">Nama panggilan</span></div></td><td valign="top" class="eb"><div class="ec">Tompo</div></td></tr></table></div></div></div></div><div id="relationship"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Hubungan</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="ed dp"></div></div></div></div><div id="year-overviews"><div class="cz"><div class="da db dc dd de"><table class="df dg"><tr><td class="t dh di" aria-hidden="false"><div class="dj" role="heading" tabindex="0"><span class="dk">Peristiwa Penting</span></div></td><td class="t dl di"><div></div></td></tr></table></div><div class="dm dn"><div class="dp"><div></div><div></div><div></div><div></div><div></div></div></div></div></div><a name="footer-actions-basic"><div class="bx ee"><a href="/privacy/touch/block/confirm/?bid=100014614866494&amp;ret_cancel&amp;source=profile">Blokir orang ini</a><br /><a href="/rapid_report/?context=%7B%22session_id%22%3A%22984823fc-9c60-4c01-b7b3-29821375e0ca%22%2C%22type%22%3A2%2C%22story_location%22%3A%22profile_someone_else%22%2C%22entry_point%22%3A%22profile_report_button%22%2C%22entry_point_uri%22%3A%22%5C%2Faldhy.tompo%3Flst%3D100003433068177%5Cu00253A100014614866494%5Cu00253A1573644042%22%2C%22reportable_ent_token%22%3A%22100014614866494%22%7D&amp;av=100003433068177">Cari Dukungan atau Laporkan Profil</a></div></a></div><div style="display:none"></div></div></div><div class="cz"><div class="ef eg"><div id="search_div"><form method="get" action="/search/"><input type="hidden" name="search" /><input type="hidden" name="search_source" value="footer" /><div class="eh ei"><table class="m ej ek" role="presentation"><tbody><tr><td class="n el em"><label for="u_0_3"><img src="https://static.xx.fbcdn.net/rsrc.php/v3/yC/r/hwCZEAcHQnm.png" width="20" height="20" class="en s" role="presentation" /></label></td><td class="t eo em"><input class="v dc ep eq" aria-label="Cari" name="query" id="u_0_3" type="text" /></td><td class="n er em"><input value="Cari" type="submit" class="z ba bb es bd" /></td></tr></tbody></table></div></form></div><table class="m" role="presentation"><tbody><tr><td class="t et" style="width:50%"><a class="eu" href="/pages/?ref=Footer&amp;ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo">Halaman Anda</a><a accesskey="0" class="eu" href="/help/?ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo">Bantuan</a><a accesskey="7" class="eu" href="/settings/?entry_point=mbasic_footer_link&amp;ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo">Pengaturan &amp; Privasi</a></td><td class="t ev" style="width:50%"><a class="eu" href="/bugnub/?source=Footer&amp;ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo">Laporkan Masalah</a><a accesskey="8" class="eu" href="/policies/?ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo">Ketentuan &amp; Kebijakan</a><a class="eu" href="/logout.php?h=AfdzPVB07vM3sCQL&amp;t=1573644042&amp;button_name=logout&amp;button_location=footer&amp;source=mbasic_logout_button&amp;ref_component=mbasic_footer&amp;ref_page=%2Fwap%2Fprofile_timeline.php%3Ainfo" id="mbasic_logout_button">Keluar (Fatonah)</a></td></tr></tbody></table><a class="ew" href="#header">Kembali ke Atas</a></div></div></div></div></body></html>

所以,有人可以帮我解决这个问题吗?如何找到 class 'cr cs' 中的所有元素? 以及如何解决这个问题,如果我只想在 class 'cr cs'?

中找到一个元素

为什么会出现此错误?因为你的脚本在整页中找不到 Lainnya 我也尝试了当我在页面中看到没有 Lainnya 文本时我得到了同样的错误

解决方案:

更改您想要的文本,例如 ALdhy

或其他原因,因为当我访问此页面时,我看到有 ALdhy 和其他名称,而不是 Lainnya 名称,所以这就是您收到此错误的原因

错误显示正确,因为它在您的页面中找不到 Lainnya

调用 button_lainnya.get_attribute('class') returns class 列表 的元素,其中有两个 class 分隔一个 space 个字符。

get_element_by_class_name(Class) 的调用预计会给出一个 单个 class 名称 --- "cr" 或 "cs",但不是两者。

如果您需要使用两个 class 名称查找元素,则需要使用 CSS 选择器:

driver.find_element_by_css_selector(".cr.cs")

我的 python 有点生疏,但这应该可以工作:

selector = "." + Class.replace(" ", ".")
driver.find_element_by_css_selector(selector)