How to get span value without class on python selenium - python

I'm new on python, just started learning, I try to get value span on other website and I'm trying to get value on this picture on red mark , but I still get an error.
Snapshot of the HTML:

To print the text 4 hours 30 minutes you can use either of the following Locator Strategies:
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element(By.CSS_SELECTOR, "div.lab-preamble__details.subtitle-headline-1 > span").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//div[#class='lab-preamble__details subtitle-headline-1']/span").text)
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 and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.lab-preamble__details.subtitle-headline-1 > span"))).text)
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='lab-preamble__details subtitle-headline-1']/span"))).get_attribute("innerHTML"))
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 relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

If you're using only selenium to find element you can try to get this element using find_element_by_xpath.
To get xpath fast and easy you can right-click element in Chrome DevTools and in "Copy" section there is the "Copy xpath" option.

Related

Copied xpath directly from chrome, and not working

Trying to scrape odds from fanduel, goal is to get the player's name. In this case Jayson Tatum.
https://sportsbook.fanduel.com/basketball/nba/philadelphia-76ers-#-boston-celtics-31137202?tab=player-points
Even when I copy the xpath directly from chrome it doesnt seem to work. Though it works when I hardcode and look for an element through xpath containing the text Jayson Tatum.
This is my code
name = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH,'//*[#id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')))
Also tried this
name = driver.find_element(By.XPATH, '//*[#id="root"]/div/div[2]/div[1]/div/div[2]/div[3]/div/div[2]/div/div[3]/div[1]/div/div/div[1]/span')
Still get a NoSuchElement trying both ways.
To print the text Jayson Tatum you can use the following Locator Strategy:
Using xpath and text attribute:
print(driver.find_element(By.XPATH, "//span[text()='UNDER']//following::div[1]//span").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategy:
Using XPATH and get_attribute("innerHTML"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='UNDER']//following::div[1]//span"))).get_attribute("innerHTML"))
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 relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Is there a way to get the value within a <div> using selenium in python?

I'm new to python and webscraping so I'm not sure what the name of the value inbetween the <div>'s in an element is called. Sorry for not being able to specify.
<div class="syllable">value</div>
Is there a way to have the value inbetween the <div>'s get assigned to a string variable in python using selenium using XPath?
For example, the "value" in the element would be a string and it would print out:
value
I'm new to python and selenium so I can't figure it out.
To print out the text of the element.
elem=driver.find_element_by_class_name("syllable")
print(elem.text)
xpath:
elem=driver.find_element_by_xpath("//div[#class='syllable']/text()")
print(elem)
it is called html innerText
you can retrieve this value using text in selenium , or get_attribute.
This returns the rendered text (means displayed text)
elem=driver.find_element_by_class_name("syllable")
print(elem.text)
This return the text with out checking the style attribute meaning returns value even if its not displayed in UI
elem=driver.find_element_by_class_name("syllable")
print(elem.get_attribute("textContent")
you can find elem using this text also:
// partial match
elem=driver.find_element_by_xpath("//div[contains(text(),'value')])
print(elem.text)
// exact match
elem=driver.find_element_by_xpath("//div[text()='value')])
print(elem.text)
// exact match of the elements text if there is any child element like span it won't return the element
elem=driver.find_element_by_xpath("//div[.='value')])
print(elem.text)
Also note:
Other things you could read about outerHTML , innerHTML
To print the text value you can use either of the following Locator Strategies:
Using class_name and get_attribute("textContent"):
print(driver.find_element_by_class_name("syllable").get_attribute("textContent"))
Using css_selector and get_attribute("innerHTML"):
print(driver.find_element_by_css_selector("div.syllable").get_attribute("innerHTML"))
Using xpath and text attribute:
print(driver.find_element_by_xpath("//div[#class='syllable']").text)
Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:
Using CLASS_NAME and get_attribute("textContent"):
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "syllable"))).get_attribute("textContent"))
Using CSS_SELECTOR and text attribute:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.syllable"))).text)
Using XPATH and get_attribute():
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='syllable']"))).get_attribute("innerHTML"))
Console Output:
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
You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python
References
Link to useful documentation:
get_attribute() method Gets the given attribute or property of the element.
text attribute returns The text of the element.
Difference between text and innerHTML using Selenium

Following-sibling in XPath using Python and Selenium

I can't figure it out how to find xpath using following-sibling for those marked in green in the pic. Basically I want to click on the button called "Open" which is on the same row as the text "front".
Try this XPath:
//td[strong="front"]/following-sibling::td//a[.="Open"]
P.S. As you didn't provide HTML sample as text (do not provide it as picture!!!) I can't see whether there is a text in <i>...</i> node
So you also can try
//td[strong="front"]/following-sibling::td//a[contains(., "Open")]
Try this
//strong/parent::td/following-
sibling::td/descendant::i/following-sibling::text()
[contains(.,'Open')]
The above xpath is assuming the level of Open text and i is of same level
You can use this XPath:
//td[strong="front"]/following-sibling::td//a[contains(text(), "Open")]
As per the HTML you have shared the element with text as front doesn't have any siblings. So you may not be able to use following-sibling.
Solution
To click on the button with text as Open which is on the same row with the element with text as front you you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following xpath based Locator Strategies:
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[#class='btn default ']"))).click()
Using XPATH along with the text:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='front']//following::td[1]//a[contains(., 'Open')]"))).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

List not printing all .text values using Selenium and Python

So I've been using selenium recently and decided to pull some values into a list using the following:
Values = browser.find_elements_by_xpath("//div[#class='main-col']//li[#class='test-item test-item--favourites']")
The values are all stored as expected. Though when I use for example:
for i in range (len(Values)):
print(Values[i].text)
i+=1
The code runs up to about the 50th index in the list and outputs the details, but after this its blank. I can run:
print(values[50])
It will output but .text gives me nothing.
Im using pYcharm for the development and looked into the variables at this stage. I can see that once I click on the variable it states 'collecting variable data' and after this I can then output some more of the variables in the list.
Is there a way I can force selenium to collect all the variable data so I can loop through each index in the list and store it in the .text format or is there a better alternative/method I can use?
If the .text is blank that means that there is no text between CSS Selector. To get an attribute out of a CSS Selector you have to use the get_attribute() function. Inside the () you place a string which is the name of an attribute which value you are looking for. for example
print(i.get_attribute('class'))
You are able to extract the text till the 50th index as find_elements_by_xpath() was able to identify 50 odd elements as per your Locator Strategy.
Solution
To collect all the desired elements using Selenium and python you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR and get_attribute("innerHTML"):
print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.main-col li.test-item.test-item--favourites")))])
Using XPATH and text attribute:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[#class='main-col']//li[#class='test-item test-item--favourites']")))])
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 to click through onclick event with in a table through Selenium and Python

I am automating a function where the user needs to click on the link highlighted in the below link.
HTML
I tried a contains on the xpath which is //*[#id="lc"], and an onclick option from Here neither of which work and error with the element doesn't exist error.
I know I am in the right iframe, because when using the xpath with no contains the script clicks the first xpath link with no issue.
To click() on the desired element as the element is a dynamic element 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, "td.EMROtherEpsEven>table tr>td.EMROtherEpsEven#lc[onclick*='Hemoglobin']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[#class='EMROtherEpsEven']/table//tr/td[#class='EMROtherEpsEven' and #id='lc'][contains(#onclick, 'Hemoglobin')]"))).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
I have not use Selenium with Python, but I have with java, and in Java you have to use simple quote (') instead the double quote ("), or you can just a simpler locator id=lc without any quotes, if you're sure the ID is correct this should work.
i have done this in java hope you find the answer
yes in static or in dynamic tables the id will be the same but if you try using absolute xpath
this are two absolute path of the table element resides each other in first row
/html[1]/body[1]/section[1]/section[1]/div[1]/data[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[1]
/html[1]/body[1]/section[1]/section[1]/div[1]/data[1]/div[1]/div[4]/div[1]/div[1]/div[1]/div[1]/div[2]/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]
This are the relative xpath of the same elements
//a[contains(text(),'AF17OT41603')]
//div[#id='1547533504703-0-uiGrid-000C-cell']//div[#class='ui-grid-cell-contents ng-binding ng-scope'][contains(text(),'AKSHAY PATIL')]
Table

Categories