Python Webelement to .text selenium - python

I use Selenium,
I would like to know how to retrieve the two values ​​result.name and result.prix under value 'text' and be able to save in text format I find myself with the code below with two webelement value which is displayed only on my console but I can not save them.
Thanks for your help.
from selenium import webdriver
driver=webdriver.Chrome(executable_path="chromedriver.exe")
driver.get('https://www.ldlc.com/')
barre_de_recherche=driver.find_element_by_id("search_search_text")
barre_de_recherche.send_keys('Processeur socket 1200')
bouton_recherche=driver.find_element_by_class_name("submit")
bouton_recherche.click()
recuperation_nom=driver.find_elements_by_class_name("title-3")
recuperation_prix=driver.find_elements_by_class_name("price")
for resultat_nom in recuperation_nom:
print(resultat_nom.text)
for resultat_prix in recuperation_prix:
print(resultat_prix.text)

Related

Unable to parse data with BeautifulSoup: Python3

[Please click here to view the Tags][1]
The following is one of the tables of the website I am scraping. Here, under 'tbody' I wish to click on the 'MS' button tag under both odd and even class which provides me a different table for further parsing it.
I am using Selenium and Python 3 to perform Web scraping.
The current code only clicks on the 'MS' button in the first row. How can I create a for loop so that I can iterate through all the rows and click on 'MD' element in all the rows?
Thank you.
Following is the code:
table_0=table.find_element_by_tag_name('tbody')
for buttons in table_0.find_elements_by_tag_name("tr"):
buttons.find_elements_by_xpath('//tr[#class="odd"]')
buttons.find_element_by_xpath('//button[text()="MS"]').click()
for buttons in table_0.find_elements_by_tag_name("tr"):
buttons.find_elements_by_xpath('//tr[#class="even"]')
buttons.find_element_by_xpath('//button[text()="MS"]').click()
You should be able to use a CSS selector to gather those for clicking
.btn-group.btn-group-xs button:first-child
The selector certainly works:
Not sure whether you need waits but maybe something like:
elements = driver.find_elements_by_css_selector(".btn-group.btn-group-xs button:first-child")
for element in elements:
element.click()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get("https://ibl.mdanderson.org/fasmic/#!/")
driver.find_element_by_css_selector("input[type='text']").send_keys("AKT1 (3 mutations)")
driver.find_element_by_css_selector("input[type='text']").send_keys(Keys.RETURN)
elements = driver.find_elements_by_css_selector(".btn-group.btn-group-xs button:first-child")
for element in elements:
element.click()

Can't find text element with selenium

I'm trying to use Selenium to create a new message in my mailbox. I have a problem with finding napisz (en: 'write') button on my e-mail website. I tried to use driver.find_element_by_link_text but it doesn't work. I've managed to go workaround this problem using xpath but I'm very curious why the first method fails.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://profil.wp.pl/login.html?zaloguj=poczta&url=https://poczta.wp.pl/profil/')
elem_login = browser.find_element_by_name('login_username')
elem_login.send_keys('stack_scraper_wp#wp.pl')
elem_password = browser.find_element_by_name('password')
elem_password.send_keys('thankyouforhelp')
elem_zaloguj_button = browser.find_element_by_id('btnSubmit')
elem_zaloguj_button.click()
browser.get('https://poczta.wp.pl/d635/indexgwt.html#start')
elem_napisz_button = browser.find_element_by_link_text('napisz')
elem_napisz_button.click()
EDIT: I've tried to used same xpath today but it failed. Is it possible that it's somehow dynamic causing the problem?
.find_element_by_link_text() looks for a elements only. In your case, this is the button element and cannot be located using this locator.

Extracting hidden element in Selenium

I have an element of type hidden in an iframe. I am wondering if there would be any way to get this value as I am using selenium. More specifically it is a captcha field. I've tried pulling it with something along the lines of
#!/usr/bin/env python
from selenium import webdriver
driver=webdriver.Chrome(chrome_bin_path)
driver.get('http://websitehere.com')
print driver.find_element_by_xpath('//*[#id="recaptcha-token"]').text
but because of it's hidden nature it returns nothing.
Below is a snippet of the source.
Highlighted is the string of interest. (value)
driver.switch_to_frame('undefined')
token_value = driver.find_element_by_id('recaptcha-token').get_attribute('value')
driver.switch_to_default_content()
Moving between windows and frames.
Use this method
hidden_text = element.get_attribute("textContent")

Use Selenium to enter search text into Google

I want to go to Google, and type in "Hello world" using Selenium + Python.
from selenium import webdriver
import time
import json
driver = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs')
driver.get("http://www.google.com")
Everything up until this part works fine. But now I'm having trouble understanding how I'd select their text field and enter text. The searchbar has an ID #gbqfq, but the below code returns an error.
driver.webElement('body', 'gbqfq')
Error:
'WebDriver' object has no attribute 'webelement'
Here are the docs:
http://selenium-python.readthedocs.org/en/latest/api.html#module-selenium.webdriver.remote.webelement
There are more than one way to find an Element. In your case, the easiest approach would be find_element_by_id. Your code would look like:
driver.find_element_by_id()
There are many other ways to find an element, such as find an element by Xpath, by CSS selector and etc.
I am not really sure why you had a "webElement" in your code, is it a typing mistake?

Selenium Webdriver with Python - obtaining the string or contents in between html tags

I'm new to Selenium's webdriver and Python. I know about this article about getting the HTML source, but I don't want the entire HTML line for a DOM object, just the content in between the tags. I also looked at webdriver source code for help on finding this button DOM object
for example:
<button id = "picview">Pic View</button>
How do I just get "Pic View"?
Also, using get_attribute("button id"), How would I get this specific button id as there are multiple buttons on the page with button id?
For example:
picbox_elem_attr = picbox_elem.get_attribute('button id')
print picbox_elem_attr
How do I ensure that picbox_elem_attr variable is set to the "picview" button and not some other button?
I don't have a
driver.find_element_by_name("xxx")
or a
driver.find_element_by_id("aaa")
to use to find this button.
To get text of an element use the text property. For example:
text_inside_button_id = driver.find_element_by_id("picview").text
Here is some additional documentation to help you with the webdriver binding library.
http://goldb.org/sst/selenium2_api_docs/html/
I forgot about xpath. Oops!
driver.find_element_by_xpath('//*[#id="picview"]')
and then you can right-click the object and use xpath within the dev tools.

Categories

Resources