Transferring content from a div class to an input class

I am seeking help to extract text from a div class and transfer it to an input class.

Here is the code I have written:

import os
import time
from selenium import webdriver
from pyvirtualdisplay import Display
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

lol = input("Click to print div class text on console...")

driver = webdriver.Firefox()
driver.get("<any link to verification code")

# The xpath of the div class.
verification_code_div = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[2]")))
# And this is the xpath of the input class.
verification_code_input = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[1]/input")))

Therefore, my goal is to retrieve the text from the div class and input it into the input class.

Answer №1

To achieve this, you can utilize the .get_attribute() method,

# Here is the xpath for the specified div class.
verification_code = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "/html/body/table/tbody/tr/td/table/tbody/tr[4]/td/table/tbody/tr/td/table/tbody/tr[2]/td[2]/table/tbody/tr[2]/td[2]")))

input_text = verification_code.get_attribute("innerText")

# This is the xpath for the input class.
verification_code = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/section/main/div/div/div[1]/div[2]/form/div/div[1]/input")))

verification_code.send_Keys(input_text)

Answer №2

# Retrieving verification code
verification_code = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "xxxxx"))).text
# Entering verification code
input_verification_code = WebDriverWait(driver, 60).until(EC.presence_of_element_located((By.XPATH, "xxxxx"))).send_keys(verification_code)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In the world of Python and Trio, where producers also double as consumers, the question arises: how can one elegantly exit when

My goal is to create a basic web crawler using trio and asks. I am utilizing a nursery to launch multiple crawlers simultaneously, and a memory channel to store a list of urls to be visited. Each crawler is given copies of both ends of the channel so they ...

Adding a .PHP File to Two Separate DIVs

I am using wordpress to create a custom theme. I'm interested in placing all the content from my slider.php file inside a div box on my index.php page. How would I go about doing this? SLIDER.PHP <div class="slider"> // All the image tags wit ...

What methods can be used to modify the appearance of the cursor depending on its position?

Is there a way to change the cursor to a left arrow when on the left half of the screen and switch it to a right arrow when on the right half, using JavaScript? I am trying to achieve something similar to what is shown on this website. I attempted to acco ...

Is the `document.documentElement` consistently defined and always representing the HTML element?

I am looking to make changes to the <html> element using a script within the <head> section of an HTML page. My goal is to only access and modify the <html> element itself, without affecting any of its children. Should I wait for the DOM ...

Tips for transferring a jQuery array to PHP

I am encountering an issue when trying to send a jQuery array to PHP. Initially, I have one form in HTML and upon clicking 'add', I end up with two forms. Afterwards, I input data into the form which is then stored in a jQuery array. However, I a ...

Viewing YouTube videos on an iPhone can be done through a web view

I am having some trouble playing videos in this specific format. http://www.youtube.com/embed/lNOMZoF9VlM?rel=0 Here is my HTML string. NSString *html = [NSString stringWithFormat:@"<html><head>\ <body s ...

Implementing a keypress function that can handle duplicate names

HTML <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="pm" type="text" value="0"/> <input name="total" type="text" value="0" disabled="disabled"/> Javascript $('[name="pm"]' ...

Changing the height of one Div based on another Div's height

I currently have a display of four divs positioned side by side. I am seeking a solution to ensure that the height of each div remains consistent, and should adjust accordingly if one of them increases in size due to added text content. For reference, ple ...

Issue encountered during the execution of the project on wamp server

I am working on a PHP 5 website and have downloaded all the files from the server to my WAMP for editing some text. However, when I try to run localhost, I encounter an error: **Internal Server Error The server has encountered an internal error or miscon ...

Obtaining the text content of a <div> element when clicked using Selenium

I am trying to extract the email address from the code snippet below, but I am unsure of how to do it. Any assistance would be greatly appreciated! <div class="taLnk hvrIE6 fl" onclick="ta.trackEventOnPage('Listing', 'Email', 774169 ...

When working with angular.js and angular-sanitize.js, the src attribute is removed from JSON HTML data

I'm new to Angular and I'm really enjoying learning how to work with it. Currently, I have a simple JSON file that contains text structured like this: "gettingstarted":{ "title":"Getting Started", "content":"<img ng-src='i ...

What is the best way to restrict page access only to localhost using Python?

When setting up a server using Python's SimpleHTTPServer, how can I restrict access so that the server only responds to requests from the local machine? Is there a way to achieve this in a language-independent manner? Perhaps by assigning a specific ...

My navigation menu has a nested ul, but on mobile devices, it doesn't display all the items in the list. What could be causing

When I click on "Products" in my main navigation, only the first 6 items are displayed from a nested ul. How can I make all of them display when the user clicks on "Products"? Is this a CSS issue or a problem with the script? Here's a screenshot for r ...

What is the best way to create a plot of a two-dimensional random walk using Python?

Recently, I developed a code for a two-dimensional random walk: def r2walk(T): x = np.zeros((T)) y = np.zeros((T)) x = [0]*T y = [0]*T for t in range(0,T): walk = random.random() if 0 < walk < .25: x[t ...

Steps to resolve background image problems

Currently encountering issues with the application where the background image is not showing up behind the login form. This problem arises while using a bootstrap template for the website. Attempted to set the background image in the .main-content div with ...

What is the reason behind this HTML/CSS/jQuery code functioning exclusively in CodePen?

I have encountered an issue where this code functions properly in JSFiddle, but not when run locally in Chrome or Firefox. I suspect there may be an error in how the CSS or JavaScript files are being linked. In the Firefox console, I am receiving an error ...

Hovering over the Laravel Breeze dropdown will activate a dropdown feature

Recently, I've been working on a project with Laravel Breeze and have been utilizing some default components. The dropdown component used in the navigation bar caught my attention. By default, it requires a click for the menu to drop down below. Howev ...

Creating an HTML file with a Windows-inspired icon and file name design using CSS

I searched everywhere on the internet but couldn't find a solution. If anyone knows of a similar link to my question, please share it with me. I am trying to achieve a design similar to Windows icons and file names in HTML using CSS. Please refer to ...

Attempting to eliminate the parent container of a slide generated by the Slick Slider Plugin

I have developed a filter mechanism that hides specific classes within a slick slider based on the data-tag associated with the objects and the value of checkboxes. However, when these classes are hidden, they still occupy space because I should be hiding ...

The image fails to appear

Check out my website at www.wostokhr.pl I am having trouble getting the picture "pics/hero.jpg" to show up in the "div id="hero"" section with a JS parallax function. I have double-checked the HTML and CSS validators and everything seems to be correct. An ...