Selenium Web Driver |查找对象|蟒蛇

时间:2017-06-04 19:20:53

标签: python selenium selenium-webdriver

事先我为我的英语道歉,因为我是用口译员写的。

有一项任务是使用Selenium Web Driver(Python,Chrome)点击其中一个帐户,假设AAccount。 问题是在XPath和CSS上不可能这样做,因为元素是相同的。 以下是该页面源代码的一部分。我还附上了页面的截图。

enter image description here



</style>
  </head>
  <body>
  <div class="wrapper">
  <div class="gb_ag" ng-non-bindable="" id="gb"><div class="gb_hf gb_fg"><div class="gb_ib gb_fg gb_R gb_eg"><div class="gb_7d gb_R gb_fg gb_7f"><div class="gb_Q gb_R gb_S gb_fg"><span class="gb_P gb_R">TeAOK</span></div></div><div class="gb_sc gb_fg gb_R"><div class="gb_ga" id="gbsfw" style="background-color:#eee;min-width:376px"></div><div class="gb_Ac gb_Wc gb_R gb_Bc"><div class="gb_Dc"><a class="gb_b" aria-haspopup="true" role="button" tabindex="0"><div class="gb_Ec gb_Fc gb_4b"></div></a><div class="gb_lb"></div><div class="gb_kb" style="border-bottom-color:#eee"></div></div><div class="gb_ga" aria-hidden="true" aria-live="assertive"></div></div><div class="gb_fb gb_Wc gb_fg gb_R"><div class="gb_Dc gb_hb gb_fg gb_R"><a class="gb_b gb_db gb_R" href="https://accounts.google.com/SignOutOptions?hl=ru&amp;continue=https://accounts.google.com/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline" title="Аккаунт Google: TeAOK G9T3arz  &#10;(teaokg9t3arz@gmail.com)" role="button" tabindex="0"><span class="gb_8a gbii"></span></a><div class="gb_lb"></div><div class="gb_kb"></div></div><div class="gb_mb gb_ga" aria-label="Информация об аккаунте" aria-hidden="true"><div class="gb_pb"><a class="gb_qb gb_tf gb_sb" aria-label="Изменить картинку профиля" href="https://profiles.google.com/?hl=ru"><div class="gb_tb gbip" title="Профиль"></div><span class="gb_jb">Изменить</span></a><div class="gb_rb"><div class="gb_ub gb_vb">TeAOK G9T3arz</div><div class="gb_wb">teaokg9t3arz@gmail.com</div><div class="gb_ob"><a href="https://myaccount.google.com/privacypolicy">Конфиденциальность</a></div><a class="gb_Fa gb_qf gbp1 gb_xb" href="https://myaccount.google.com/?utm_source=OGB&amp;utm_medium=act">Мой аккаунт</a></div></div><div class="gb_Cb"><div class="gb_Db gb_6a" aria-hidden="true"><a class="gb_Eb gb_Mb" href="/b/0/DelegateAccountSelector?continue=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Fauth%3Fclient_id%3D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%26redirect_uri%3Dhttps%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php%26scope%3Dhttps%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube%26response_type%3Dcode%26access_type%3Doffline&amp;authuser=0"><img class="gb_Ob gb_sb" src="data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" alt="Профиль" data-src="https://lh3.googleusercontent.com/-Yg4816hNGF4/AAAAAAAAAAI/AAAAAAAAAAA/AAyYBF6hOAK-q02pBDqts2Bde8yxxNX1wQ/s48-c-mo/photo.jpg"><div class="gb_Hb"><div class="gb_Pb">TeAOK G9T3arz</div><div class="gb_Qb">teaokg9t3arz@gmail.com (по умолчанию)</div></div></a></div><a class="gb_Sb gb_6a" href="https://myaccount.google.com/brandaccounts?authuser=0&amp;continue=https://accounts.google.com/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline&amp;service=/b/0/DelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline%26authuser%3D%24authuser" aria-hidden="true"><span class="gb_Tb gb_4b"></span><div class="gb_Ub">Все аккаунты брендов &raquo;</div></a></div><div class="gb_yb"><div><a class="gb_Fa gb_pf gb_xb" href="https://accounts.google.com/AddSession?continue=https%3A%2F%2Faccounts.google.com%2Fb%2F0%2FDelegateAccountSelector%3Fcontinue%3Dhttps%253A%252F%252Faccounts.google.com%252Fo%252Foauth2%252Fauth%253Fclient_id%253D702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com%2526redirect_uri%253Dhttps%253A%252F%252Fvkmix.com%252Fmodules%252Fyoutube%252Fbot2.php%2526scope%253Dhttps%253A%252F%252Fwww.googleapis.com%252Fauth%252Fyoutube%2526response_type%253Dcode%2526access_type%253Doffline">Добавить аккаунт</a></div><div><a class="gb_Fa gb_rf gb_yf gb_xb" id="gb_71" href="https://accounts.google.com/Logout" target="_top">Выйти</a></div></div></div></div></div></div><div class="gb_Zb gb_R gb_3b" id="gbq1" style="max-width:152px;min-width:152px"><div class="gb_0b"><a class="gb_3d gb_2b" href="/" title="Мой аккаунт" data-ved="0EMQuCAQ"><span class="gb_Za gb_4b"></span></a></div></div></div><div id="gbw"></div></div><div class="gb_sd" id="gba"></div>
  <div class="delegate-account-selector main content clearfix">
  <div class="sign-in">
  <div class="signin-box">
  <h2 id="signin-action">Выберите аккаунт</h2>
  <ol class="accounts" id="account-list">
  <li id="account-teaokg9t3arz@gmail.com">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=none">
  <span class="account-image" id="account-image-0"></span>
  <span class="account-name">TeAOK G9T3arz</span>
  <span class="account-email" id="account-email-0">teaokg9t3arz@gmail.com</span>
  <span class="select-account text-icon-select" id="account-account-0">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=105773503169388790046">
  <span class="account-image" id="account-image-1"></span>
  <span class="account-name">EAccount</span>
  <span class="select-account text-icon-select" id="account-account-1">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=116942572818460775716">
  <span class="account-image" id="account-image-2"></span>
  <span class="account-name">FAccount</span>
  <span class="select-account text-icon-select" id="account-account-2">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=110025695516281123129">
  <span class="account-image" id="account-image-3"></span>
  <span class="account-name">GAccount</span>
  <span class="select-account text-icon-select" id="account-account-3">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=100327913140968503320">
  <span class="account-image" id="account-image-4"></span>
  <span class="account-name">CAccount</span>
  <span class="select-account text-icon-select" id="account-account-4">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=114686295257876098243">
  <span class="account-image" id="account-image-5"></span>
  <span class="account-name">DAccount</span>
  <span class="select-account text-icon-select" id="account-account-5">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=101169419531313874972">
  <span class="account-image" id="account-image-6"></span>
  <span class="account-name">iAccount</span>
  <span class="select-account text-icon-select" id="account-account-6">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=116191438938972075200">
  <span class="account-image" id="account-image-7"></span>
  <span class="account-name">AAccount</span>
  <span class="select-account text-icon-select" id="account-account-7">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=106861284959818641724">
  <span class="account-image" id="account-image-8"></span>
  <span class="account-name">BAccount</span>
  <span class="select-account text-icon-select" id="account-account-8">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=104823728391082992328">
  <span class="account-image" id="account-image-9"></span>
  <span class="account-name">IAccount</span>
  <span class="select-account text-icon-select" id="account-account-9">Выберите</span>
  </a>
  </li>
  <li id="account-">
  <a href="https://accounts.google.com/o/oauth2/auth?client_id=702580890038-5fs7raaanbrl7rssnkg3e62qgprukbni.apps.googleusercontent.com&amp;redirect_uri=https%3A%2F%2Fvkmix.com%2Fmodules%2Fyoutube%2Fbot2.php&amp;scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&amp;response_type=code&amp;access_type=offline&amp;pageId=102918689899435112584">
  <span class="account-image" id="account-image-10"></span>
  <span class="account-name">JAccount</span>
  <span class="select-account text-icon-select" id="account-account-10">Выберите</span>
  </a>
  </li>
  </ol>
  </div>
  </div>
  <div class="side-content">
  <div class="product-headers">
  <h1 class="redtext">Аккаунты</h1>
  </div>
  <div class="select-account">
  <p>
  Выберите аккаунт Google
  </p>
  <ul>
  <li>
  TeAOK G9T3arz
  (teaokg9t3arz@gmail.com)
  </li>
  <li>
  EAccount
  </li>
  <li>
  FAccount
  </li>
  <li>
  GAccount
  </li>
  <li>
  CAccount
  </li>
  <li>
  DAccount
  </li>
  <li>
  iAccount
  </li>
  <li>
  AAccount
  </li>
  <li>
  BAccount
  </li>
  <li>
  IAccount
  </li>
  <li>
  JAccount
  </li>
  </ul>
  </div>
  </div>
  </div>
<div class="google-footer-bar">
  <div class="footer content clearfix">
  <ul id="footer-list">
  <li>Google</li>
  <li>
  <a  href="https://accounts.google.com/TOS?loc=RU&hl=ru" >Конфиденциальность и условия</a>
  </li>
  <li id="footer-help"><a  href="https://support.google.com/accounts?hl=ru" >Справка</a></li>
  </ul>
  </div>
</div>
  <script type="text/javascript">
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

您可以尝试找到所需的元素by partial link text

driver.find_element_by_partial_link_text("AAccount").click()

如果它不起作用,您可以尝试添加一些时间来等待所需的元素:

from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

wait(driver, 10).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "AAccount"))).click()