I have googled this extensively but can't get to an answer, I'm trying to extract "90,856" in the title. It's value changes over time:
<div class="card hreddeep">
<div class="card-header hbuilt">
<div class="card-body">
<div class="row">
<div class="col-6 text-left">
<div class="h6 text-uppercase param-title">Search<br>Volume</div>
<div class="h4 param-content" title="90,856">
my code is:
find_element_by_xpath("//div[#class='h4-param-content']").get_attribute("title")
grateful for some help. Thanks
Your XPath contains a typo (according to your sample data) : h4-param instead of h4 param...
First, test with :
find_element_by_xpath("//div[#class='h4 param-content']").get_attribute("title")
If it doesn't work, try with an expected condition.
Add the following imports :
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Code :
wait = WebDriverWait(driver, 10)
output = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='h4 param-content']"))).get_attribute("title")
print(output)
Related
I am trying to copy the text area message into a variable so that I can parse it.
The text area has message as follows:
<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><SubmitResponse xmlns="http://eftr</Error></SubmitResponse></Body></Envelope>
The HTML is as follows:
<textarea class="v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly" rows="5" tabindex="-1" readonly="" style="width: 100%; height: 100%;"></textarea>
I am trying the following and other configurations but I have not success:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
full_xpath = r'/html/body/div[1]/div/div[2]/div/div[2]/div/div[2]/div/div/div[3]/div/div/div[2]/div/div[2]/div/div/div/div/div[3]/textarea'
path_to_chromedriver = r'C:\chromedriver' # change path as needed
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
.........
.........
content = browser.find_element_by_xpath(full_xpath).text
I am not sure how to achieve the above.
Basically print(content) should produce the following:
<?xml version='1.0' encoding='UTF-8'?><Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><SubmitResponse xmlns="http://eftr</Error></SubmitResponse></Body></Envelope>
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
textcontent=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//textarea[#class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']"))).get_attribute("value")
in textarea text is stored as value,
an example text area:
<!DOCTYPE html>
<html>
<body>
<h1>The textarea element</h1>
<form action="/action_page.php">
<label for="w3review">Review of W3Schools:</label>
<textarea id="w3review" name="w3review" rows="4" cols="50">
At w3schools.com you will learn how to make a website. They offer free tutorials in all web development technologies.
</textarea>
<br><br>
<input type="submit" value="Submit">
</form>
<p>Click the "Submit" button and the form-data will be sent to a page on the
server called "action_page.php".</p>
</body>
</html>
you can use above example html and in console try
$('textarea').value
use following xpath to idetify the element.
content = browser.find_element_by_xpath("//textarea[#class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']").text
Or use get_attribute("innerHTML")
content = browser.find_element_by_xpath("//textarea[#class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']").get_attribute("innerHTML")
I would suggest use WebDriverWait() and wait for visibility_of_element_located()
content=WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//textarea[#class='v-textarea v-widget v-readonly v-has-width v-has-height v-textarea-readonly']"))).text
you need to import below libraries
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am a newbie in selenium, and I got stock when I want to click the button with the next html code :
<div class="mb-8">
<div class="wrapper">
<button class="w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ">
<div class="font-medium">
<div class="text-17 md:text-18 md:font-bold leading-18">Activate</div>
<div class="text-13 md:text-12 font-normal md:font-medium leading-normal">Mining 4 hours</div>
</div>
</button>
</div>
</div>
This is what I tryed :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import time
driver = webdriver.Chrome("chromedriver.exe")
driver.get("https://link")
search = driver.find_element_by_id("email")
search.clear()
search.send_keys("username")
search = driver.find_element_by_id("password")
search.clear()
search.send_keys("password")
search.send_keys(Keys.RETURN)
time.sleep(3)
link= driver.find_element_by_link_text("Miner")
link.click()
time.sleep(3)
link= driver.find_element_by_class_name("w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ")
Got this:
InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=88.0.4324.182)
link=driver.find_element_by_css_selector("w-full h-14 pt-2 pb-1 px-3 bg-accent text-dark-1 rounded-full md:rounded select-none cursor-pointer md:hover:shadow-big focus:outline-none md:focus:bg-accent-2 md:focus:shadow-small ")
Same error.
Tryed also this (because of the info in the image)
link=driver.find_element_by_css_selector(".w-full.h-14.pt-2.pb-1.px-3.bg-accent.text-dark-1.rounded-full.md\:rounded.select-none.cursor-pointer.md\:hover\:shadow-big.focus\:outline-none.md\:focus\:bg-accent-2.md\:focus\:shadow-small.")
Same error.
Tryed this also:
link= driver.find_element_by_class_name("Activate")
Got this :
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".Activate"}
(Session info: chrome=88.0.4324.182)
Thank you for your help !!!
If none of these work your element is in an iframe. driver.switch_to.frame() would solve it.
Xpaths:
//div[#class='wrapper']/button
//div[.='Activate']/parent::button[1]
Css selectors:
button.w-full.h-14.pt-2.pb-1.px-3.bg-accent.text-dark-1.rounded-full.md:rounded.select-none cursor-pointer.md:hover:shadow-big.focus:outline-none.md:focus:bg-accent-2 .md:focus:shadow-small
If you want to find an element by class then just use one class instead of all classes.
Here in your case 'Activate' is not a class. You can try:
link= driver.find_element_by_class_name("w-full");
Also, you can find this button using XPath.
If you're still struggling with this task I have posted the answer on a different post Find the button element for selenium Automation in python be sure to check it out.
I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my testing.
<div class="gallery-list">
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
I need get value of attribue by xpath sl-video-preview
Some can help us.
thks
Here is the generic xpath that you can use.
//figure[#class='figure hd']/picture
And you have to get 'sl-video-preview' attribute.
Chrome Console Output:
To extract the value of the attribue sl-video-preview, as the element is an Angular element 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(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.gallery-list > figure.figure.hd > a > picture.ng-isolate-scope.sl-safe"))).get_attribute("sl-video-preview"))
Using XPATH:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='gallery-list']/figure[#class='figure hd']/a/picture[#class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
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 problem, why in some cases the tag figure appear multiple times and this example by #DebanjanB get only one record, then I need all results.
<div class="gallery-list">
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure hd" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<figure class="figure" ng-class="profileGallery.css" profile-item-remove="9>
<a href="https://#" data-login="" gallery-modal="9" rel="nofollow">
<picture sl-video-preview="https://movie.mp4" sl-safe="" class="ng-isolate-scope sl-safe">
</a>
</figure>
<div>
XPath Example that works:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='gallery-list']/figure[#class='figure hd']/a/picture[#class='ng-isolate-scope sl-safe']"))).get_attribute("sl-video-preview"))
How do to get all records?
BeautifulSoup is not extracting the div I want properly. I am not sure what I am doing wrong. Here is the html:
<div id='display'>
<div class='result'>
<div>text0 </p></div>
<div>text1</div>
<div>text2</div>
</div>
</div>
And here is my code:
div = soup.find("div", {"class": "result"})
print(div)
I am seeing this:
<div class="result">
<div>text0 </div></div>
What I am expecting is this:
<div class="result">
<div>text0</div>
<div>text1</div>
<div>text2</div>
</div>
This works as expected if I remove the </p> tag. In other words, the </p> tag seems to be throwing the parser off.
Edit:
This works as expected on Python 2.7.12, beautifulsoup4 version 4.5.1. But does not work on Python 3.6.4, beautifulsoup4 version 4.7.1. Not sure if the culprit is python version or bs4 version (more likely).
Can someone please help?
I see no problem using select
from bs4 import BeautifulSoup as bs
html = '''
<div id='display'>
<div class='result'>
<div>text0 </p></div>
<div>text1</div>
<div>text2</div>
</div>
</div>
'''
soup = bs(html)
soup.select('.result')
I am trying to click on a button which is inside nested DIVs.
Below is the html
<div class="chat-message-container ngi bot" chat-msg-id="EzPtItD3exi2lTGS3SQkV0-h|0000016" chat-msg-text="What is the intended purpose of your investment">
<div class="message-bubble">
<div class="message-text"><p>What is the intended purpose of your investment</p>
</div>
>
</div>
<div class="attachment-container">
<div id="0attachment" style="display: block" class="attachment">
<div class="attachment-info">
<div class="attachment-title"></div>
<p class="attachment-subtitle-1"></p>
<p class="attachment-subtitle-2"></p>
<div class="carousel-counter-container">
<button type="button" id="0prev" class="carousel-btn-ngi" data-atura-carousel="prev"><</button>
<p class="carousel-counter" }"="">1/1</p>
<button type="button" id="0next" class="carousel-btn-ngi" data-atura-carousel="next">></button>
</div>
</div>
<div class="action-button-container">0 - 3 years3 - 5 yearsover 5 years
</div>
</div></div>
</div>
I need to click on button 0 - 3 years
I have tried to click the elemet using xpath as below
driver.findElement(By.xpath("//*[#id='0attachment']/div[2]/a[1]")).click();
it did not work. I think it is because of the nested structure. I read about the switch method. But i dont think that can be used to switch inside another DIV
Kindly help
To click() on the element with text as 0 - 3 years need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "0 - 3 years"))).click()
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "0 - 3 years"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-button-container>a.link-as-button.quick-reply[data-button-display-value='0 - 3 years']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='action-button-container']/a[#class='link-as-button quick-reply' and contains(., '0 - 3 years')]"))).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