How to locate an element and extract required text with Selenium and Python

pioniere

I am using Selenium to enter data on a web page, but have run into an issue with one of the input fields. This is the HTML code causing my difficulty:

<div class="form-group">
<label for="address">Enter your address</label>
<input type="text" name="address" class="form-control" value="" style="font-size:1.8em;padding:15px;height:40px">
<label for="address">Enter this code: 2784873 </label>
<input type="text" name="code" class="form-control" value="" style="font-size:1.8em;padding:15px;height:40px">

I want to use Selenium to copy the numeric value after Enter this code: 2784873 (in this case, 2784873), which I would then enter in the input field below, but so far have been unable to figure out how to get this. I tried using driver.find_element_by_id() but that was unsuccessful. I also tried using xpath (which seems to me like the best approach) with:

codes = driver.find_elements(By.XPATH, "//*[@id=\"page-wrapper\"]/div[2]/div[2]/center/form/div/label[2]")

but the value returned in codes is:

<selenium.webdriver.remote.webelement.WebElement (session="ae4e91d8ec7e2bc161", element="0.570523580858733-2")

Can anyone suggest a way to do this?

QHarr

You can use

driver.find_element_by_css_selector('.form-control + [for=address]').text

Use replace() to remove the enter this code: string if required

That is a class selector "." with adjacent sibling combinator joining to attribute = value selector. So element with attribute for having value address that is adjacent to element with class form-control.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to locate the element and extract the innerText using Selenium and Python

How to locate an element by class name and its text in python selenium

Selenium: How to locate the element with text Run Insight

How to locate the href element using selenium python

How to locate the element using Selenium Python

How to locate style element in Selenium Python?

How to locate the span element with Xpath Selenium and Python

How I locate this element with Selenium (Python)?

How to locate this <a> element with Selenium?

Python Selenium - How to extract element based on text inside span tag?

How to extract the text within the element using Selenium WebDriver and Python?

How to locate next element after text found in b tag - Selenium Python

Unable to locate text input element using selenium and python

How to locate the element with text as My Name as per the HTML through Selenium

Xpath Selenium- How to locate a element by the sub text

How to locate an element using Selenium by its visible text

unable to locate element in selenium in python

Unable to locate element selenium Python

Unable to locate element with python and selenium

Selenium Python Unable to locate element

Unable to locate element with Selenium Python

Unable to locate element _ Python Selenium

Python Selenium cannot locate element

selenium how to locate this type of element?

How locate the pseudo-element ::before using Selenium Python

How to locate the button element using Selenium through Python

How to locate the element within an iframe using Selenium WebDriver through Python

How to locate an element with multiple classnames using Selenium and Python

How to locate an element on the webpage using Selenium and Python 3.6