0

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()

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

9 Comments

Hey, thanks for responding. I tried that but it didn't seem to work, I'll edit my post to show all of my code so far in case its something else.
Yes the error I get is too long for a comment, here it is: pastebin.com/07JRHd6W
@TheOneTrueSign My bad, it should be driver.find_element_by_class_name("_WGh") and driver.find_element_by_class_name("vk_arc"). I fixed the answer and added one more option
Thanks, but sadly none of these are making the translation tab drop down. Could an issue be somewhere else in my code?
@TheOneTrueSign Its seconds. It will look for the elemnts up to 10 seconds every time you do driver.find_element(). You can increase or decrease the time of course.
|
1

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()

4 Comments

Thank you for the advice, however for me that code doesn't seem to make the drop down bar expand. Maybe it is a problem somewhere else in my code?
Sorry about that, I took the wrong id. I updated the answer.
Thanks, that works. If I may ask, I see where you got "#uid_1" But where did you get "> div[role='button']" from. Is that standard to include after you enter the id?
I got the div[role='button'] from the element where the mouse is supposed to click. It's an attribute selector (developer.mozilla.org/en/docs/Web/CSS/Attribute_selectors). And yes it's common practice to use a combinator after an id.
1

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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.