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.
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.
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.
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.
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With