简体   繁体   English

如何找到元素硒

[英]how to find element selenium

How do I find 我如何找到

 element = driver.find_element_by_id("id","class","class")

Im trying to click an ad doing direct with xpath will not work: 我试图点击直接使用xpath的广告无效:

/html/body/div/div[1]/div[1]/div/a/img

Traceback (most recent call last):
File "a.py", line 14, in <module>
element = driver.find_element_by_id("/html/body/div/div[1]/div[1]/div/a/img")

HTML shown as follows: HTML如下所示:

</head> 
<body scroll="no">
   <div id="widget" class="widget">
   <div class="plug">
   <div class="thumbBorder">
   <div class="thumb">
   <div class="ton" style="display: block;">
      <div class="title_bg"> </div>
      <a class="title q" target="_blank" href="//prwidgets.com/t/ghxa/g0us/7433c239e19107a4301ad9959d2d37440/aHR0cDovL3Ry‌​aXBsZXh2aWQuY29tLw==">Kiss N Tell</a> 
   </div>
   <a class="q" target="_blank" href="//prwidgets.com/t/ghxa/g0us/7433c239e19107a4301ad9959d2d37440/aHR0cDovL3Ry‌​aXBsZXh2aWQuY29tLw=="> <img title="Title" src="//prstatics.com/prplugs/0/747604/160x120.jpg"

find_element_by_id in Selenium python binding accepts one parameter which is the value of the id attribute. Selenium python绑定中的find_element_by_id接受一个参数,它是id属性的值。 Such as login_form = driver.find_element_by_id('loginForm') Please refer to the doc here Addition to that you can use 例如login_form = driver.find_element_by_id('loginForm')请参考此处的文档

driver.find_element(By.ID, 'your ID')

In this case you can try xpath - and axis ie following-sibling 在这种情况下,您可以尝试xpathaxisfollowing-sibling

element = driver.find_element_by_xpath("//a[class='q']/following-sibling::img[1]")
element.click()

NB I have assumed there is no a with class name q in the whole html doument. 注:我假设没有a与类名q在整个HTML doument。

This may not work for you but when there is not an easy ID or NAME to grab, I go into the browser (I will refer to Firefox) right click on the element, select 'Inspect Element', then right click on the highlighted area in the inspection window and select 'Copy Unique Selector'. 这可能对您不起作用,但是当没有容易获取的ID或名称时,我进入浏览器(我将指代Firefox),右键单击该元素,选择“检查元素”,然后右键单击突出显示的区域在检查窗口中,然后选择“复制唯一选择器”。 Then you can paste this into your code and use: 然后,您可以将其粘贴到您的代码中并使用:

selector = 'pasted string here'
element = driver.find_element_by_css_selector(selector) 
element.click()

EDIT: using the selector provided by @James below: 编辑:使用下面@James提供的选择器:

selector = 'div.plug:nth-child(1) > div:nth-child(1) > div:nth-child(1) > a:nth-child(2) > img:nth-child(1)'
element = driver.find_element_by_css_selector(selector)
element.click()

This usually works quite well for me. 这通常对我来说效果很好。

EDIT: Add a real example. 编辑:添加一个真实的例子。 Try this and see if it works. 试试这个,看看是否可行。

# open google search page and click the "About" link

from selenium import webdriver

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('www.google.com/ncr')
# got the selector below using Firefox 'Inspect Element -> Copy Unique Selector'
about_selector = 'a._Gs:nth-child(3)' 
about = driver.find_element_by_css_selector(about_selector)
about.click()

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

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