how to get a dynamic value from a website using selenium C#?
I have a problem, I want to write a code in selenium C# that getting dynamic value from a webpage and display in my APP, The value of the variable within the webpage changes frequently and I want to display this value in my APP,I use a textbox to display variable value, this is my code:
textBox2.Text=driverA.FindElement(By.Id("stock_ClosingPrice")).Text;
but its a static text, and it does not display changes to the value of the variable ("stock_ClosingPrice") I don't really know that I can use selenium for this or not!? can everyone help me? thanks a lot
1 answer
-
answered 2021-01-11 06:59
Dylan Lacey
Calling the
.Text
method returns the value of the innerText property of an element.For example, for this HTML:
<p id="demo"> This element has extra spacing and contains <span>a span element</span>.</p>
The innerText is
This element has extra spacing and contains a span element.
. This is not the HTML of the element though; it's literally just the text. Notice the extra spaces have been removed, as has the span.(See the W3C Documentation for more; That's where I got that example from.)
If you're after the value entered into a text box, you need to make two changes. Firstly, the value of a text box isn't part of the HTML, which is why calling
.Text
wasn't working. You need to use the getAttribute method to get the Value of the, uh, Value attribute:element.getAttribute("value");
Secondly, I don't think you're selecting the right element. Your XPath is finding the first div, not the first input element. If you're after a text box, one a user could alter, you're not going to find it with that selector. I'd suggest either using the element ID, or using an input field selector, eg
input[@type='text']
See also questions close to this topic
-
Why do we need checking return values of all system calls?
I'm review the Australian Government Information Security Manual. In this document, Secure programming practices are described as follows: Platform-specific secure programming practices are used when developing software, including using the lowest privilege needed to achieve a task, checking return values of all system calls, validating all inputs and encrypting all communications.
My question is why do we need checking return values of all system calls? What should we do in c#?
Thanks!
-
Self referencing a class in C# Unity
I'm trying to create a class that'll model a conversation.
[System.Serializable] public class Choice { // Just a sentence to show what kind of choice player is making public string choiceLine; // The whole answer that will be given public Sentence choiceDetailed; // The Answer/Response NPC will give to our Choice public Sentence npcAnswer; // If last answer of the NPC is given, go back to the main page private bool isLastAnswer; public Choice[] choices; }
But I think there's a problem with the self referencing class, is there a better way to do this? I want to nest Choices inside one Choice object
-
Is there a way to update the Segoe UI Emoji font on Windows Server 2019
I have an C# web application that uses the Segoe UI Emoji font to put an emoji in a PDF that is generated. When generating the PDF on my development PC the PDF appears correct. After deploying and running on the server the emoji is the wrong one. I believe I've tracked the problem down to different versions of the font being installed.
On my Windows 10 (20H2) development PC the Segoe UI Emoji font is at version 1.29.
On the Windows Server 2019 (1809) the same font is at version 1.27.
The emoji giving me issues is the smiling face with horns (devil). I'm using 0x1F608 to add the character to my output using the emoji example from here.
How do you convert a single Unicode code point to a string at runtime?
https://emojipedia.org/smiling-face-with-horns/
I've been looking for a way to update the font on the server, but due to it being a protected system font I'm really struggling. I can't delete it or install the newer version by copying the font file from Windows 10 to the server.
I've been researching and trying a few things to deal with protected system fonts, making a registry change to unlock the font and installing/copying the font but then it was promptly overwritten with the old version due to it being stored/cached in the WinSXS folder.
I went down a road trying to replace the file in the WinSXS folder but that turns into a permission nightmare and just feels wrong.
Is there a way to update that font without waiting around for Microsoft to push out an update?
-
Java - How to check PC sleep mode (Quartz Scheduler + Selenium)
Currently I am using Quartz Scheduler to schedule an automated (Selenium-based) job at certain time (let say 3PM) everyday. The job is to automate a health declaration form on a website using ChromeDriver.
The problem occurs when the PC is in sleep mode at that certain time for few minutes. Then, (even if PC is unlocked), the whole PC will be BSOD Critical Process Died every time when the PC is been awake.
I have tried multiple ways but there is seems no clear tutorial on how to check if PC is in Sleep mode or not. I know lots of people suggest JNA but I cant seems to get good understanding of how to set it up.
Is there any workaround this issue?
-
scraping python : existing connection was forcibly closed by the remote host
im scraping from this website https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx to get kurs price table.it looks like im getting blocked by that website.
with selenium and bs4,but when im trying to get the table i got error like this
ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
and this is my code
driver = webdriver.Chrome() driver.get("https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx") wait = WebDriverWait(driver, 10) driver.implicitly_wait(10) #secs # click "usd" book = wait.until(EC.element_to_be_clickable((By.ID,"selectPeriod"))) sel = Select(book) sel.select_by_value("range") bookk = wait.until(EC.element_to_be_clickable((By.ID,"ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_ddlmatauang1"))) sel = Select(bookk) sel.select_by_value("USD ") driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") start_date = driver.find_element_by_id("ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_txtFrom") start_date.send_keys("20-Nov-15") end_date = driver.find_element_by_id("ctl00_PlaceHolderMain_g_6c89d4ad_107f_437d_bd54_8fda17b556bf_ctl00_txtTo") end_date.send_keys(time.strftime("%d-%m-%Y")) time.sleep(5) buttons = driver.find_elements_by_xpath("//input[@value='Cari']") buttons[1].click() src = driver.page_source # gets the html source of the page headers = { #"Referer": "https://id.investing.com/commodities/gold-historical-data", "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0", "X-Requested-With": "XMLHttpRequest" } parser = BeautifulSoup(src,"lxml") # initialize the parser and parse the source "src" url = "https://www.bi.go.id/id/statistik/informasi-kurs/transaksi-bi/Default.aspx" r = requests.get(url, headers=headers) html = r.text table = parser.find("table", attrs={"class" : "table1"}) # A list of attributes that you want to check in a tag) rows = table.find_all('tr') data = [] for row in rows[1:]: cols = row.find_all('td') cols = [ele.text.strip() for ele in cols] data.append([ele for ele in cols if ele]) result = pd.DataFrame(data, columns=['nilai', 'kurs_jual', 'kurs_beli', 'tanggal']) result.to_csv("kurs1.csv", index=False) df = pd.read_csv("kurs1.csv") pd.set_option('display.max_rows', df.shape[0]+1) print(df)
what should i do? please help me,actually a month ago i already succeed but suddenly the class,id from that website changed so i have to change it all.when i try to run it again i got connection error.im stuck since weeks ago! thank you in advance the csv file shouldve been like this
-
How to get user input while Multiprocessing
I am trying to run multiple selenium instances in wich I need to enter captchas, but I am a beginner in multiprocessing.
So while running and its time to give input it shows an error:
EOFError: EOF when reading a line
Here is an example of the code I am running:
import time from selenium import webdriver import multiprocessing def first(): chromedriver = "C:\chromedriver" driver = webdriver.Chrome(chromedriver) driver.set_window_size(1000, 1000) driver.get('https://www.google.com/') time.sleep(5) captcha1 = input("in1: ") print(inn) def sec(): chromedriver = "C:\chromedriver" driverr = webdriver.Chrome(chromedriver) driverr.set_window_size(1000, 1000) driverr.get('https://www.google.com/') captcha2 = input("in2: ") print(ins) if __name__ == '__main__': p1 = multiprocessing.Process(target=first) p2 = multiprocessing.Process(target=sec) p1.start() p2.start() p1.join() p2.join()
Not only do I need to know how to give input but in this instance the 'captcha2' input would be needed first, so the 'captcha1' would have to wait until 'captcha2' is given...
Thank you in advance to anyone that decides to help!
-
How can I loop through Excel files with different sheet names and with the data starting sometimes on A3 vs A1?
I am not good at For Loops, so the more detailed the answer, the better (also new at Python). Please also ask for any clarification. I cant share much code or data because it is private data, but I can recreate if needed for demo purposes.
I have 24 excel files which contains data for each month for the last 2 years. In a folder path 2019, the excel files have names like REPORT_201901, REPORT_201902, etc up to REPORT_201912. In another file path named 2020, it has the same basics, but named different (THE_REPORT_202001 and so on).
I need to loop through each excel file, but the excel sheets have different names and on some sheets, the data starts on A3 instead of the top at A1 because someone put a title on the first couple rows.
I currently have all the code typed out and working, but I manually have to import each file to a dataframe and then append to a master CSV.
ALL THE DATA and COLUMNS ARE THE SAME, so I know my code works on each file after I manually specify which row to start on and sheet name to pull.
How can I write this loop and apply my code to each file?
TLDR: I need to loop through excel files that have different sheet names and also deal with the records starting on different rows. The data is in different file paths, and is always 24 months of data.
-
Tab-complete a parameter value based on another parameter's already specified value
This self-answered question addresses the following scenario:
Can custom tab-completion for a given command dynamically determine completions based on the value previously passed to another parameter on the same command line, using either a parameter-level
[ArgumentCompleter()]
attribute or theRegister-ArgumentCompleter
cmdlet?If so, what are the limitations of this approach?
Example scenario:
A hypothetical
Get-Property
command has an-Object
parameter that accepts an object of any type, and a-Property
parameter that accepts the name of a property whose value to extract from the object.Now, in the course of typing a
Get-Property
call, if a value is already specified for-Object
, tab-completing-Property
should cycle through the names of the specified object's (public) properties.$obj = [pscustomobject] @{ foo = 1; bar = 2; baz = 3 } Get-Property -Object $obj -Property # <- pressing <tab> here should cycle # through 'foo', 'bar', 'baz'
-
calculate sum of values dynamically using offset in Excel
I have a table that looks like this
TOTAL SCORE SPORT Basketball Volleyball Football Hockey 14 SCORE 3 2 6 3
I want to be able to dynamically work out the SUM of the score using the OFFSET function (not excel table)
=SUM(B2:E2)
=SUM(OFFSET(B2,0,0,1,5))
both of these formulas are not dynamic as if I add an extra score or 10 new scores it cant calculate the sum of this
How can I make this dynamic
=SUM(OFFSET(B2,COUNTA(2:2),0))
THIS works however is there a more efficient way of doing this?
-
ssl_client_socket_impl.cc(962) handshake failed
i'm trying to run my project(that I written it on my macBook) on my windows server.it use selenium and chrome web driver -v 87 when its run it opens web driver and start browsing and clicking but it doesn't catch anything and just print a writing and an repeated error:
dev tools listening on ws: //127.0.0.1:51103/devtools/browser/...:[Error:device_event_log_impl.cc(211)]Bluetooth: bluetooth_adapter_win GetBluetoothAdapterStaticsActivationFactory failed: Class not registered [Error: ssl_client_socket_impl.cc(962)] handshake failed: returned -1, SSL error code 1, ror -100
I don't know what should I do. my program was working correctly on my mac! do you have any IDEA?
-
How to monitor cookies in Selenium webdriver in python
With
driver.get_cookies()
I can get all actual cookies. But I wonder if it's possible to also monitor the changes when the visited website adds new cookie or change/delete any cookie. -
chrome 88 can't redefine webdriver
I'm trying to used
Selenium
execute_cdp_cmd to set navigator.webdriver undefined, it's not working on Chrome version 88, the error is "Can't redefinewebdriver
;" but it's working well on Chrome 77 or below version. is anybody find this problem? and how to fix it?the code is below:
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })" })
-
Not able to Use Selenium to open a menu item
I have the following HTML in a page I am automating.
<div class="menucomponent" id="patientsmenucomponent" name="Patients" onclick="OpenMenu({ "MENUNAME": "patients", "MENUOBJ": { "MENUNAME": "Patients", "COLUMNS": [ { "CATEGORIES": [ { "ITEMS": [ { "ITEMONCLICK": "", "ITEMTARGET": "Main", "ITEMURL": "/client/registration.esp", "VENDORSHORTCUTKEYS":
I would like to open the first menu item. I tried a couple of things with no luck.
I could locate the main menu. However, not able to click the first menu item. Appreciate your help.
-
selenium ide chrome how to save to csv file
I am currently using selenium ide for chrome and I was wondering if it's possible to save text from a given class to a csv file.
I try the command csvSave but Selenium Ide doesn't recognize it.
I did store id="some class name" !csvLine which give me no error. How can I get this input to save into a csv file.
-
sendKeys() does not pass the character "@"
I am using selenium 3.6.0. and I run some basic test (login to the Stackoverflow page). But, when I am sending email, character "@" is not passed. Instead of it, it looks like for me that it puts some values from buffer.
package Cucumerframework.steps; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.junit.Assert; import cucumber.api.java.Before; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class StepsClass { WebDriver driver; @Before() public void setup() { System.setProperty("webdriver.chrome.driver", "C:\\Users\\WCodeKemalK\\Cucumber\\Cucumerframework\\target\\test-classes\\Resources\\chromedriver.exe"); this.driver = new ChromeDriver(); this.driver.manage().window().maximize(); this.driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); } @Given("^User navigates to stackoverflow website$") public void user_navigates_to_stackoverflow_website() throws Throwable { driver.get("https://stackoverflow.com/"); } @Given("^User clicks on the login button on homepage$") public void user_clicks_on_the_login_button_on_homepage() throws Throwable { driver.findElement(By.xpath("/html/body/header/div/ol[2]/li[2]/a[1]")).click(); } @Given("^User enters a valid username$") public void user_enters_a_valid_username() throws Throwable { driver.findElement(By.xpath("//*[@id=\"email\"]")).sendKeys("testmail@gmail.com"); } @Given("^User enters a valid password$") public void user_enters_a_valid_password() throws Throwable { driver.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("xxxxxxx"); } @When("^User clicks on the login button$") public void user_clicks_on_the_login_button() throws Throwable { driver.findElement(By.xpath("//*[@id=\"submit-button\"]")).click(); } @Then("^User should be taken to the successful login page$") public void user_should_be_taken_to_the_successful_login_page() throws Throwable { Thread.sleep(3000); WebElement askQuestionsButton = driver.findElement(By.xpath("//a[contains (text(), Ask Question)]")); Assert.assertEquals(true, askQuestionsButton.isDisplayed()); } }
Here is what I get: https://ibb.co/k35WL3N