TheOneTrueSign
TheOneTrueSign

Reputation: 149

Clicking Specific button with Selenium

I am trying to click a particular button with Selenium in Python, but am having trouble identifying that particular button. For example, if I was on the google page of this, and I wanted to have the translation bar drop down, how would I go about referencing that specific element. Inspecting it in my browser I see some of what I assume to be its data as:

    <div style="clear: both;" aria-controls="uid_0" aria-expanded="false"
     class="_LJ _qxg xpdarr _WGh vk_arc" data-fbevent="fastbutton" jsaction="kx.t;
 fastbutton: kx.t" role="button" tabindex="0" data-ved="0ahUKEwiwn-6K17XLAhVLWD4KHTk9CTkQmDMILzAA">

However, from this point I'm not sure how I would use the find element by functions to reference what I need to in order to call it properly.

driver.find_element_by_*("?").click()

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

#comment
print ("Let's talk about Python.")


driver = webdriver.Firefox()

driver.get("http://www.google.com")

assert "Google" in driver.title

elem = driver.find_element_by_name("q")

elem.send_keys("ignominious")
elem.send_keys(Keys.RETURN)

driver.find_element_by_*("?").click()


assert "No results found." not in driver.page_source

driver.close()

Upvotes: 0

Views: 4144

Answers (3)

Shubham Jain
Shubham Jain

Reputation: 17593

Do you want to click on arrow. If yes then below code is working for me:-

    driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
    driver.get("https://www.google.com/");

    driver.findElement(By.name("q")).sendKeys("ignominious");
    driver.findElement(By.name("q")).sendKeys(Keys.RETURN);

    driver.findElement(By.className("vk_ard")).click();

Hope it will help you :)

Upvotes: 1

Florent B.
Florent B.

Reputation: 42538

For a better maintainability you should try to work with ids.

With your example the selector would be:

driver.find_element_by_css_selector("#uid_1 > div[role='button']").click()

Upvotes: 1

Guy
Guy

Reputation: 50919

You can use css_selector with the class attribute

driver.find_element_by_css_selector("._LJ._qxg.xpdarr._WGh.vk_arc").click()

Or class_name with any one of the classes

driver.find_element_by_class_name("_LJ").click()
# or
driver.find_element_by_class_name("_qxg").click()
# or
driver.find_element_by_class_name("xpdarr").click()
# or
driver.find_element_by_class_name("_WGh").click()
# or
driver.find_element_by_class_name("vk_arc").click()

Sending click to the element child will also work

driver.find_element_by_class_name("vk_ard").click()

Upvotes: 1

Related Questions