Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use By class in selenium with python

I've written a code to test a page in python with selenium. The code works but now I'm trying to rewrite it better.
I have created a locators.py file as described in the following documentation: selenium python binding - page objects.
For simple locators this is straightforward. This is what I've done so far:

from selenium.webdriver.common.by import By

class PhotoHomePageLocators(object):
"""A class for photographer home page locators. All photographer interface home page locators should come here"""
add_new_project_btn = (By.CLASS_NAME, 'addProject')
view_all_projects_btn = (By.CLASS_NAME, 'viewAllProjects')


class NameCoverPageLocators(object):
"""A class for name and cover home page locators. All name and cover page locators should come here"""
project_name_txt_box = (By.XPATH, '//*[@id="rform_pt2_0"]')
client_name_txt_box = (By.XPATH, '//*[@id="rform_pt2_1"]')
client_email_txt_box = (By.XPATH, '//*[@id="rform_pt2_2"]')

but I have the following line in my code which I'm uncertain how to write so it fits the above syntax:

driver.find_element_by_class_name('coverUpload').find_element_by_class_name('pButton').click()

I'm locating a class within a class, there are several pButton classes in the page, but only one inside the coverUpload class.

like image 814
ShaiS Avatar asked Aug 14 '16 15:08

ShaiS


People also ask

How do I select a class in Selenium Python?

We can select a drop-down menu option value with Selenium webdriver. The Select class in Selenium is used to handle drop-down. In an html document, the drop-down is identified with the <select> tag. Let us see the html structure of a drop-down.

What is the use of by class in Selenium?

Initializes a new instance of the By class using the given functions to find elements. Gets or sets the value of the description for this By class instance. Gets or sets the method used to find a single element matching specified criteria. Gets or sets the method used to find all elements matching specified criteria.

Why is the ActionChains class used?

action_chains. ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions.


2 Answers

You should be able to arrange the line you are asking about as follows:

someName = driver.find_element_by_class_name('coverUpload')
ButtonName = someName.find_element_by_class_name('pButton')
ButtonName.click()
like image 129
Ath0s Avatar answered Sep 24 '22 23:09

Ath0s


instead of 2 class search maybe you can use relative Xpath if That element is unique.

elem = driver.find_element(By.XPATH, '//*[@class="coverUpload"]//*[@class="pButton"]')
elem.click()
like image 31
George Imerlishvili Avatar answered Sep 22 '22 23:09

George Imerlishvili