0

I have such a html page as below:

<div id="abc" class="MyClass" data-placement-name="mysearch">
   <div class="A" title="class A">
   <div class="separator"></div>
   <span class="btn" onclick="return placementEvCall('abc', 'def', event, this);">
      ::before
   </span>
...
...

Now I want to do the action of onclick of btn.

I've tried to driver.find_element_by_id('abc') and it worked as expected.

But I don't know how to get and click the btn object, which is a span class in div.


UPDATE
enter image description here

Yves
  • 11,597
  • 17
  • 83
  • 180
  • 1
    xpath - "//div[@id='abc']/span" – santhosh kumar Jun 26 '17 at 06:02
  • @Yves, What actually you want to do ? are you trying to click on that `span` only ? or want to get the value return by`onclick="return placementEvCall('abc', 'def', event, this);` this method ? – NarendraR Jun 26 '17 at 06:13
  • @Tuks I just need to click it. – Yves Jun 26 '17 at 06:14
  • @Yves, OK . Then have you tried the xpath written by Santosh kumar in comment ? – NarendraR Jun 26 '17 at 06:15
  • @Tuks well, I've tried like this: `driver.find_element_by_xpath("//div[@id='abc']/span")`, but I get an error: `Message: no such element: Unable to locate element` – Yves Jun 26 '17 at 06:20
  • @Yves, First check weather you element is in iframe if yes then first switch into it. If not then put some explicit wait until visibility or clickable the element – NarendraR Jun 26 '17 at 06:24

2 Answers2

1

Try with xpath with a value //div[@id="abc"]//span[@class="btn"] as follows:

driver.find_element_by_xpath('//div[@id="abc"]//span[@class="btn"]').click()

Let me know if this Answers your Question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Your answer should work well but not for my case. I just put the real case in my question. I'm trying to get the selected part and do the click action. I've tried like this: `driver.find_element_by_xpath('//div[@id="taplc_masthead_search_0"]//span[@class=".ui_icon.search.cye-lm-tag"]').click()` or `driver.find_element_by_xpath('//div[@id="taplc_masthead_search_0"]//span[@class="search"]').click()` or `driver.find_element_by_xpath('//div[@id="taplc_masthead_search_0"]//span[@class="ui_icon search cye-lm-tag"]').click()`. Nothing works. – Yves Jun 26 '17 at 08:17
  • If possible, you could try yourself: `https://www.tripadvisor.cn/`. I'm trying to simulate a click on the search icon on the top at right corner. – Yves Jun 26 '17 at 08:19
0

You can simply use

In [1]: from selenium import selenium

In [2]: from selenium import webdriver

In [3]: from selenium.webdriver.common.keys import Keys

In [4]: from selenium.webdriver.firefox.webdriver import FirefoxProfile

In [5]: browser = webdriver.Firefox()

In [6]: browser.get('file:///home/pooja/example_page.html')

In [7]: btn = browser.find_element_by_class_name('btn')

In [8]: btn.cl
btn.clear  btn.click  

In [8]: btn.click()

For your reference

Pooja
  • 1,254
  • 14
  • 17
  • Now I get such an error: `Message: invalid selector: Compound class names not permitted` – Yves Jun 26 '17 at 06:14
  • @Yves you can take a look at https://stackoverflow.com/questions/32043877/compound-class-names-not-permitted-error-webdriver for your error message – Pooja Jun 26 '17 at 06:26
  • In fact you are right. Go to see the capture in my question. You will find that there are three class names. I was trying to `find_element_by_class_name(ui_icon search cye-lm-tag)`, this is why I got the error. Now I just `find_element_by_class_name(search)` and it works. – Yves Jun 28 '17 at 01:39