I'm using Selenium and I want to grab the 'a' tag so I can navigate into my user profile. What is the best way to do this?
Here is the HTML:
<div class="parent-cont">
<div class="akd3 dafk4 dfan4">...</div>
<div class="avndkd dakdf">...</div>
<div class="fjkad fdadj dfakees">
<a aria-label="tag" class="oa2 g5iad jhuo" href="/profile.php?id=1792" role="link" tabindex="0">
<div class="dsks dssks">...</div>
<div class="dka dk2 fdakdd">...</div>
</a>
</div>
</div>
To locate the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("a[aria-label='tag'][href='/profile.php?id=1792']")
Using xpath:
element = driver.find_element_by_xpath("//a[#aria-label='tag' and #href='/profile.php?id=1792']")
Ideally, to locate the element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a[aria-label='tag'][href='/profile.php?id=1792']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#aria-label='tag' and #href='/profile.php?id=1792']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
you can use following code as mentioned:
enter code here
a = driver.find_elements_by_tag_name("a")
for a in x:
print(x.get_attribute('arial-label')
Related
Just starting to learn selenium using python and running into trouble with the basics of finding elements. This is a search/text box that I am trying to click into and send text to it.
Here is the code of the element I got from Edge.
<div class="box" id="ClinicSrch" style="display:block">
<span align="lb">Last:</span>
<input type="search" name="NameLast" size="15" class="white1" value="" maxlength="25">
First: <input type="search" name="NameFirst" size="15" class="white1" value="" maxlength="25"><br>
Here is my code:
driver.find_element(By.XPATH, '//*[#id="ClinicSrch"]/input[1]').click()
The xpath that I get when copying the element from edge inspect tool is:
//*[#id="ClinicSrch"]/input[1]
Full xpath is:
/html/body/form/div[1]/div[1]/input[1]
I've been trying with other fields and buttons where I have similar xpaths but nothing is working. I always get no such element.
To locate the Last Name search field you can use either of the following locator strategies:
Using css_selector:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
element = driver.find_element(By.CSS_SELECTOR, "input[name='NameLast']")
Using xpath:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = driver.find_element(By.XPATH, "//input[#name='NameLast']")
Ideally, to locate the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#PtSrchFrame[name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='NameLast']")))
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#id='PtSrchFrame' and #name='PtSrchFrame']")))
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='NameLast']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
How do I click this link using Selenium in Python? I've been able to get to the webpage using Selenium and click other links that have IDs. But this one doesn't have an ID or name.
HTML:
<div ng-repeat="dashboard in $ctrl.dashboards track by $index" class="btn btn-primary ng-scope active" ng-class="{active: dashboard.Dashboard_Id == $ctrl.activeDashboard.Dashboard_Id}" ng-click="$ctrl.dashboardClick(dashboard)" style="">
<span ng-bind="dashboard.Name" class="ng-binding">Hours By Activity</span>
</div>
I've tried this with no luck:
driver.find_elements_by_xpath("//*[contains(text(), 'Hours by Activity')]")
The desired element is an Angular element so to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.ng-binding[ng-bind='dashboard.Name']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='ng-binding' and #ng-bind='dashboard.Name'][text()='Hours By Activity']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Suppose there are multiple buttons in a web page
<button id='xyz'></button>
<button id='abc'></button>
<button id='pqr'></button>
I want the value of the id attributes of all the buttons using selenium
I am using this code
for button in driver.find_elements(by=By.XPATH,value='//button'):
print(button.get_attribute('id'))
I am not getting any thing in the output
Considering the HTML
<button id='xyz'></button>
<button id='abc'></button>
<button id='pqr'></button>
To extract the value of the id attributes of the <button> elements you have to induce WebDriverWait for visibility_of_all_elements_located() and using List Comprehension you can use either of the following locator strategies:
Using TAG_NAME:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.TAG_NAME, "button")))])
Using CSS_SELECTOR:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "button")))])
Using XPATH:
print([my_elem.get_attribute("id") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//button")))])
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
<div>
<div class="alk_dvImage"><img class="alk_prImg" src="https://a random photo" alt="a random product">
</div>
<div class="product-score"></div>
<a href="/products/" class="alk_prName alk_pr" title="Products Title">Strong Graphic Card
</a>
</div>
Lets assume we have a html as given above. I want to extract the title of the 'a' element which is nested in a div. And also i want the class of this same element how ever when i try this code
browser.find_element_by_css_selector('a.alk_prName alk_pr')
this does not respond anything. Btw i couldnt do anything to get tite of a element.
What happens?
Your not chaining the classes by dot in your selector, try the following:
browser.find_element_by_css_selector('a.alk_prName.alk_pr').get_attribute("title")
Example:
from selenium import webdriver
browser = webdriver.Chrome('C:\Program Files\ChromeDriver\chromedriver.exe')
html_content = """
Strong Graphic Card
"""
browser.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))
browser.find_element_by_css_selector('a.alk_prName.alk_pr').get_attribute("title")
To print the value of the title attribute i.e. Products Title you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element(By.CSS_SELECTOR, "a.alk_prName.alk_pr[href='/products/']").get_attribute("title"))
Using xpath:
print(driver.find_element(By.XPATH, "//a[#class='alk_prName alk_pr' and #href='/products/'][contains(., 'Strong Graphic Card')]").get_attribute("title"))
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.alk_prName.alk_pr[href='/products/']"))).get_attribute("value"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='alk_prName alk_pr' and #href='/products/'][contains(., 'Strong Graphic Card')]"))).get_attribute("value"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
The following is the HTML structure:
<div class='list'>
<div>
<p class='code'>12345</p>
<p class='name'>abc</p>
</div>
<div>
<p class='code'>23456</p>
<p class='name'>bcd</p>
</div>
</div>
And there is a config.py for user input. If the user input 23456 to config.code, how can the selenium python select the second object? I am using find_by_css_selector() to locate and select the object, but it can only select the first object, which is Code='12345'. I tried to use find_by_link_text(), but it is a <p> element not <a> element. Anyone can help.....
To locate the element with respect to the input by the user using Selenium and python you need to to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using variable in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='" +user_input+ "']")))
Using %s in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='%s']"% str(user_input))))
Using format() in XPATH:
user_input = '23456'
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='list']//div/p[#class='code' and text()='{}']".format(str(user_input)))))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Try the below xpath:
code = '23456'
element = driver.find_element_by_xpath("//p[#class='code' and text()='" +code +"']")