简体   繁体   English

python硒选择元素

[英]python selenium select element

I have page look like this 我的页面看起来像这样

https://imgur.com/Vfhp8N7.png https://imgur.com/Vfhp8N7.png

when I click B button then whole row hide 当我单击B按钮,然​​后整行隐藏

I need selenium to click on B button then on next B button 我需要硒才能单击B按钮,然​​后单击下一个B按钮

https://imgur.com/WGltHlG.png https://imgur.com/WGltHlG.png

but I can't figure how I try locate using xpath or class 但我不知道如何尝试使用xpath或class定位

driver.find_element_by_xpath("//*[contains(@class, 'farm_icon_b')]").click()

but it didnt work 但是没有用

I try to user array 我尝试用户数组

arr = []
driver.find_element_by_xpath("//*[contains(@class, 'farm_icon_b')]")[0].click()

but nothing too 但也没有

<a href="#" onclick="return Accountmanager.farm.sendUnits(this, 4352, 6820)" class="farm_village_4352 farm_icon farm_icon_b"></a>

I have idea to execute Accountmanager.farm.sendUnits(this, 4352, 6820) but numbers 4352, 6820 are different every time 我有想法执行Accountmanager.farm.sendUnits(this, 4352, 6820)但数字Accountmanager.farm.sendUnits(this, 4352, 6820)每次都不同

any idea please ? 有什么想法吗?

edit 编辑

i tried 我试过了

entries_count =   
len(driver.find_elements_by_css_selector(".farm_icon.farm_icon_b"))
for index in range(entries_count):
current_len = 
len(driver.find_elements_by_css_selector(".farm_icon.farm_icon_b"))
driver.find_elements_by_css_selector(".farm_icon.farm_icon_b")[entries_count - index - 1].click()
wait.until(lambda driver: len(driver.find_elements_by_css_selector(".farm_icon.farm_icon_b")) == current_len - 1)

but getting ths error 但是出现错误

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Element is not clickable at point (833, 706). 引发exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点(833,706)不可单击。 Other element would receive the click: ... (Session info: chrome=64.0.3282.186) (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64) 其他元素将获得点击:...(会话信息:chrome = 64.0.3282.186)(驱动程序信息:chromedriver = 2.35.528161(5b82f2d2aae0ca24b877009200ced9065a772e73),platform = Windows NT 10.0.16299 x86_64)

in div id linkCotainer is not interesting but here 在div ID中linkCotainer并不有趣,但是在这里

 <div id="linkContainer"> <a href="#" class="world_button_active evt-world-selection-toggle">Svět 57</a> <a href="https://forum.divokekmeny.cz" class="footer-link" target="_blank">Fórum</a> &nbsp;-&nbsp; <a href="https://help.divokekmeny.cz" class="footer-link" target="_blank">Nápověda</a> &nbsp;-&nbsp; <a href="/game.php?village=4549&amp;screen=settings&amp;mode=ticket" class="footer-link" target="_blank">Support</a> &nbsp;-&nbsp; <a href="/game.php?village=4549&amp;screen=settings&amp;mode=ref&amp;source=bottom_menu" class="footer-link">Pozvat hráče</a> &nbsp;-&nbsp; <a href="/game.php?village=4549&amp;screen=memo" class="footer-link">Poznámkový blok</a> &nbsp;-&nbsp; <a href="/game.php?village=4549&amp;screen=&amp;action=logout&amp;h=278e0f2a" target="_top" class="footer-link">Odhlášení </a> </div> 

It is telling you why it can't click on your element: 它告诉您为什么无法单击您的元素:

Other element would receive the click: <div id="linkContainer">...</div>

To troubleshoot this, first find the element that is blocking the one that you want by going to the dev tools in your browser, clicking the console tab, and looking for the element that is stealing your click: 要解决此问题,请首先转到浏览器中的开发工具,单击“控制台”选项卡,然后查找正在窃取您点击的元素,从而找到阻止所需元素的元素:

$$("div[id='linkContainer']")

If that returns an element in the console, expand it by clicking the arrow and hover over the subelements. 如果返回了控制台中的元素,请单击箭头将其展开,并将鼠标悬停在子元素上。 Do you see anything highlighting on the page? 您在页面上看到突出显示的内容吗? If so, that is what is getting your click, and you can formulate a new approach from there. 如果是这样,那就是吸引您点击的原因,您可以从那里制定新的方法。

It is very possible that something is hiding the element you want to click. 很有可能某些东西隐藏了您想要单击的元素。 You can also save yourself a lot of time if you set a debug point in your code and try to locate the element manually at the precise point where you would locate it in your script: 如果您在代码中设置了调试点,然后尝试在脚本中将元素手动定位到的精确位置,则还可以节省大量时间:

import pdb; pdb.set_trace() # This will drop you into a Python prompt
driver.find_element_by_xpath("//*[contains(@class, 'farm_icon_b')]").click()

Once your script hits the debug point, it will give you the Python prompt with all of your variables intact, so you can manipulate the browser with driver until you have discovered the problem. 一旦脚本到达调试点,它将为您提供完整的所有变量的Python提示符,因此您可以使用驱动程序操作浏览器,直到发现问题为止。

Happy hunting. 狩猎愉快。

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

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