I am trying to get the information in value=" " from this code.
As long as the class has the word orange in it, I want to store the value.
Expected result in this case is storing value "JKK-LKK" in a variable.
<input type="text" readonly="" class="form-control nl-forms-wp-orange" value="JKK-LKK" style="cursor: pointer; border-left-style: none;>
I have tried using
text = driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]").get_attribute("value"))
But I get:
AttributeError: 'list' Object has no attribute 'get_attribute'.
I've also tried getText("value") but I get is not a valid Xpath expression.
If I try only using
driver.find_elements_by_xpath("//*[contains(text(), 'nl-forms-wp-orange')]")
The list becomes empty. So I feel like I might be missing some few other key pieces.
What might I be doing wrong?
To print the value of the value attribute i.e. JKK-LKK from the elements containing class attribute as nl-forms-wp-orange you can use either of the following Locator Strategies:
Using css_selector:
print(driver.find_element_by_css_selector("input.nl-forms-wp-orange").get_attribute("value"))
Using xpath:
print(driver.find_element_by_xpath("//input[contains(#class, 'nl-forms-wp-orange')]").get_attribute("value"))
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, "input.nl-forms-wp-orange"))).get_attribute("value"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(#class, 'nl-forms-wp-orange')]"))).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
Related
This is the HTML syntax but I want to locate the element using value "ADD QUERY" using find_element_by_css_selector.
Image of HTML:
To locate the element you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = driver.find_element(By.CSS_SELECTOR, "div.global-button-primary.telemetry-button")
Using XPATH:
element = driver.find_element(By.XPATH, "//div[contains(#class, 'global-button-primary') and contains(#class, 'telemetry-button')][contains(., 'ADD QUERY')]")
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, "div.global-button-primary.telemetry-button")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class, 'global-button-primary') and contains(#class, 'telemetry-button')][contains(., 'ADD QUERY')]")))
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
My HTML format is below:
***button class="btn authorize unlocked"
span>Authenticate</span
button***
So when I use "browser.find_element_by_xpath('//span[#class = "btn authorize unlocked"]')"
to locate this button, it can not find that.
Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[#class = "btn authorize unlocked"]"}
So what should I change?
To locate the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element(By.CSS_SELECTOR, "button.btn.authorize.unlocked > span")
Using xpath:
element = driver.find_element(By.XPATH, "//button[#class='btn authorize unlocked']/span[text()='Authenticate']")
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, "button.btn.authorize.unlocked > span")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[#class='btn authorize unlocked']/span[text()='Authenticate']")))
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 class="dijitReset dijitInline dijitMenuItemLabel cpNavLeftLink dijitMenuItemSelected dijitMenuItem" data-dojo-attach-point="focusNode" role="menuitem" tabindex="-1" data-dojo-attach-event="onmouseenter:_onHover,onmouseleave:_onUnhover,ondijitclick:_onClick" aria-labelledby="dijit_PopupMenuBarItem_2_text" id="dijit_PopupMenuBarItem_2" aria-disabled="false" widgetid="dijit_PopupMenuBarItem_2" aria-haspopup="true" style="">*
<span data-dojo-attach-point="containerNode" id="dijit_PopupMenuBarItem_2_text">Carload Tools</span>
</div>
I am trying to access the following element using selenium but for some reason it saying that they are not able to recognize this element please help. My code is working fine for other elements but for this one its not able to find the element.
To identify the element you can use either of the following Locator Strategies:
Using css_selector:
element = driver.find_element_by_css_selector("div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")
Using xpath:
element = driver.find_element_by_xpath("//div[#id='dijit_PopupMenuBarItem_2' and #widgetid='dijit_PopupMenuBarItem_2']/span[#id='dijit_PopupMenuBarItem_2_text' and #data-dojo-attach-point='containerNode']")
Ideally, to locate any 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, "div#dijit_PopupMenuBarItem_2[widgetid='dijit_PopupMenuBarItem_2']>span#dijit_PopupMenuBarItem_2_text[data-dojo-attach-point='containerNode']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#id='dijit_PopupMenuBarItem_2' and #widgetid='dijit_PopupMenuBarItem_2']/span[#id='dijit_PopupMenuBarItem_2_text' and #data-dojo-attach-point='containerNode']")))
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
References
You can find a couple of relevant discussions on NoSuchElementException in:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
i have html pages, with this code :
<span itemprop="title" data-andiallelmwithtext="15" aria-current="page" aria-label="you in page
number 452">page 452</span>
i want to find the aria-label, so i have tried this:
is_452 = soup.find("span", {"aria-label": "you in page number 452"})
print(is_452)
i want to get the result :
is_452 =page 452
i'm getting the result:
is_452=none
how to do it ?
It has line breaks in it, so it doesn't match through text.Try the following
from simplified_scrapy.simplified_doc import SimplifiedDoc
html='''<span itemprop="title" data-andiallelmwithtext="15" aria-current="page" aria-label="you in page
number 452">page 452</span>'''
doc = SimplifiedDoc(html)
is_452 = doc.getElementByReg('aria-label="you in page[\s]*number 452"',tag="span")
print (is_452.text)
Possibly the desired element is a dynamic element and you can use Selenium to extract the value of the aria-label attribute inducing 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, "section#header a.cart-heading[href='/cart']"))).get_attribute("aria-label"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//section[#id='header']//a[#class='cart-heading' and #href='/cart']"))).get_attribute("aria-label"))
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 reason soup fails in doing this is because of the line break. I have a simpler solution which doesn't use any separate library, just BeautifulSoup only. I know this question is old, but it has 1k views so it's clear many people search up this question.
You can use triple-quote strings to take into account the newline.
This:
is_452 = soup.find("span", {"aria-label": "you in page number 452"})
print(is_452)
Would become:
search_label = """you in page
number 452"""
is_452 = soup.find("span", {"aria-label": search_label})
print(is_452)
Before asking, I find answer through google for 2hours. But there's no answer for me.
I use selenium with python
I apply below q/a answer to my code but nothing text printed.
XPath query to get nth instance of an element
What I want to get is "Can't select"
<li data-index="5" date="20190328" class="day dimmed">
<a href="#" onclick="return false;">
<span class="dayweek">Tuesday</span>
<span class="day">28</span>
<span class="sreader">Can't select</span>
</a>
</li>
I use xpath because I need to repeat
I should do this.
The HTML code above is a simple change
day_lists = driver.find_elements_by_xpath('//li')
Nothing printed and there's no error
for day_list in day_lists:
print(day_list.find_element_by_xpath('//span[#class="sreader"]').text)
++++ 2019/3/24/16:45(+09:00)
When I test with below code
print(day_list.find_element_by_xpath('.//span[#class="sreader"]/text()'))
Error comes out. Why there's no such element?
selenium.common.exceptions.NoSuchElementException:
Message: no such element: Unable to locate element:
{"method":"xpath","selector":".//span[#class="sreader"]/text()"}
If nothing printed and there's no error then required text might be hidden or not generated yet.
For first case you might need to use get_attribute('textContent'):
day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
print(day_list.find_element_by_xpath('.//span[#class="sreader"]').get_attribute('textContent'))
For second case:
from selenium.webdriver.support.ui import WebDriverWait as wait
day_lists = driver.find_elements_by_tag_name('li')
for day_list in day_lists:
print(wait(driver, 10).until(lambda driver: day_list.find_element_by_xpath('.//span[#class="sreader"]').text)
Note that in both cases you need to add leading dot in XPath:
'//span' --> './/span'
To print the desired text e.g. 선택불가 from the <span> tag you can write a function as follows:
def print_text(my_date):
print(driver.find_element_by_xpath("//li[#class='day dimmed'][#date='" +my_date+ "']//span[#class='sreader']").get_attribute("innerHTML"))
Now you can call the function with any date as follows:
print_text("20190328")
Here are the solutions. If this does not work then I predict the element might present either in separate window or frame.
CSS:
li[class='day dimmed'][date='20190328'] .sreader
xpath:
To check if there are multiple windows use below
print(len(driver.window_handles))
To check if there are multiple frames use below
print(len(driver.find_elements_by_css_selector('iframe')))
Try the following options:
day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[#class="day dimmed"]')))
for day_list in day_lists:
print(day_list.find_element_by_xpath('//span[#class="sreader"]').text)
OR
day_lists=WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, '//li[#class="day dimmed"]')))
for day_list in day_lists:
print(driver.execute_script("return arguments[0].innerHTML;", day_list.find_element_by_xpath('//span[#class="sreader"]')))
Please note that you need following imports as well.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By