I am trying to get text (marked by hashtags).
<div class="XYZ">
<h5>
"
#######Reports due by##############
"
<span class="hbl" data-hint="task due date">
<i class="icon-boxy-sign"></i>
</span>
</h5>
<script type="jsv#61^"></script><script type="jsv#123_"></script>
<script type="jsv#60^"></script><script type="jsv#124_"></script>
<script type="jsv#59^"></script><p>#################07/10/2020#######################</p><script type="jsv/125^"></script>
<script type="jsv/52_"></script><script type="jsv/24^"></script>
<script type="jsv/42_"></script><script type="jsv/23^"></script>
</div>
Python line to get the text inside the hashtags:
txt = dat =wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'div[class="XYZ"]'))).text
I expect the line to print: "Reports due by" and "07/10/2020, I keep getting timeoutException and Unable to locate element errors.
Seems you were close. To extract the text (marked by hashtags) you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.XYZ"))).get_attribute("title"))
Using XPATH:
print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='XYZ']"))).get_attribute("title"))
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
Here you can find a relevant discussion on selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
Change By. CSS_SELECTOR to By.XPATH and update locator to '//div[#class='XYZ']'. Should work.
Related
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
<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
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')
Access to a hosted MariaDB using a connector is not allowed by the provider. I therefore try to export some tables using a Python script with Selenium. I do not manage to find / click the export button of phpMyAdmin.
I try to locate the button using its XPATH, obtained with the Chrome browser.
I updated Chrome, the driver, Selenium to the latest versions. Attempted to make the driver wait:
(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='navigationbar']/ul[#id='topmenu']//li//img[#title='Exporteren']"))).click())
The problem is that for some reason, the button cannot be found by the driver.
I tried to search by xpath, class, css, … without success.
I do not find any frame in the html code.
Below some html code (that seems to get interpreted in the question...)
HTML:
<div class="navigationbar"><ul id="topmenu" class="resizable-menu">
<li>
<a href="server_status.php" class="tab">
<img src="themes/dot.gif" title="Status" alt="Status" class="icon ic_s_status" /> Status
</a>
</li>
<li>
<a href="server_export.php" class="tab">
<img src="themes/dot.gif" title="Exporteren" alt="Exporteren" class="icon ic_b_export" /> Exporteren
</a>
</li>
<li>
Code trials:
python
btnexp = driver.find_element_by_xpath("//*[#id='topmenu']/li[4]/a/img")
btnexp.click()
Error message:
no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='topmenu']/li[4]/a/img"}
Activation of the most recent window: driver.switch_to_window(driver.window_handles[-1])
To click() on the element with text as Exporteren you have 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, "div.navigationbar > ul#topmenu li img[title='Exporteren']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='navigationbar']/ul[#id='topmenu']//li//img[#title='Exporteren']"))).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
You can find a couple of relevant discussions in:
Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
Have you tried locating the element by Class Name?
content = driver.find_element_by_class_name('icon ic_s_status')
content = driver.find_element_by_class_name('icon ic_b_export')
My error:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"entrar"}
I have the html below:
<a role="menuitem" href="/login" class="_yce4s5">
<div class="_hgs47m">
<div class="_10ejfg4u">
<div>
Entrar
</div>
</div>
</div>
</a>
I click on the then link entrar, the following code in selenium with python:
driver.implicitly_wait(10)
element = driver.find_element_by_link_text('Entrar')
element.click()
But It raises this NoSuchElementException.
Did you try to use Upper case for the first letter? like 'Entrar' instead of 'entrar'
You might try driver.find_element_by_partial_link_text('entrar') because there is probably other text in your link. I like getting element by xpath. It's pretty easy. If you have Firefox just get the add-on xpath finder to find the xpath to any element on a web page.
To click the page link with text as Entrar as the element is a dynamic element ou have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[role='menuitem'][href='/login'] > div > div > div"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#role='menuitem' and #href='/login']//div[normalize-space()='Entrar']"))).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