我必须将从 Selenium 中提取的元素中的字符串与从文件中读取的字符串进行比较。所有的事情都很好,但是当我需要比较这两个字符串时,它没有按预期工作:
string a = "a. Check map, Help, Activate function \"Robbery\""; // Read from file
string b = "Check map, Help, Activate function \"Robbery\""; // Extracted from Selenium
我使用 Contains、IndexOf、Regex 来检查字符串 a 是否包含字符串 b,但所有这些都返回 false。我尝试在上下文之外的一个简单程序中运行它,并返回 true。它仅在我运行 Selenium 来提取文本并与从文件中读取的字符串进行比较时发生在上下文中。我在比较 Selenium 中的字符串时错了吗?我确信上面的两个字符串复制完全正确,没有任何多余或缺失的单词
我的完整代码:
public static void DoSingleQuiz(string course, int mooc, int week)
{
driver.Navigate().GoToUrl(CourseData.All[course]["MOOC" + mooc]["WEEK" + week]);
// Click on continue button
driver
.FindElement(
By.XPath("//*[@id=\"main\"]/div[1]/div[3]/div[1]/div[2]/div/div/div/div/div"),
10000
)
.Click();
var questions = driver.FindElements(By.ClassName("rc-FormPartsQuestion"), 10000);
// The keys read from file
var keys = Helper.ReadKey($"key/{course}DB/questions_and_keys.txt");
foreach (IWebElement question in questions)
{
var questionText = question
.FindElement(
By.CssSelector(
"div:nth-child(1) > div.rc-FormPartsQuestion__contentCell p:nth-child(1)"
)
)
.Text.Trim();
Console.WriteLine("===================================================");
Console.WriteLine($"testQs-{questionText}-");
// Check if the current question extracted from Selenium already exist in my keys file or not
var quesFound = keys.Find(key => key.Question.Contains(questionText));
if (quesFound != null)
{
Console.WriteLine($"sheetQs-{quesFound.Question}-");
Console.WriteLine($"sheetAns-{quesFound.Answer}-");
}
var answers = question.FindElements(By.ClassName("rc-Option"));
foreach (IWebElement answer in answers)
{
// List all the key extracted from Selenium of current question
Console.WriteLine($"testAns-{answer.Text.Trim()}-");
if (quesFound != null && quesFound.Answer.Contains(answer.Text))
{
// Choose the answer that the key string contains it
Console.WriteLine($"key-{answer.Text.Trim()}-");
answer.Click();
}
}
}
}
键是从文件中读取的测验对象列表:
public class Quiz
{
public string Question { get; set; } = string.Empty;
public string Answer { get; set; } = string.Empty;
}
internal class Helper
{
public static List<Quiz> ReadKey(string filepath)
{
List<Quiz> keys = new List<Quiz>();
try
{
string[] lines = File.ReadAllLines(filepath);
foreach (string line in lines)
{
// Split each line by the pipe character
string[] parts = line.Split('|');
if (parts.Length == 2)
{
string question = parts[0].Trim();
string answer = parts[1].Trim();
key.Add(new Quiz()
{
Answer = answer,
Question = question
});
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading file: {ex.Message}");
}
return keys;
}
}
问题和答案.txt
Which of the following are among key tasks that were identified?a. Check map, Help, Activate function "Robbery"b. Check map, Create a post, Log Outc. Customize automatic functions, Help, Log Outd. Change security code, Create a post, Check recent events | a. Check map, Help, Activate function "Robbery"
Could you run user tests with this prototype and receive useful data to inform your designs?a. Yes, any type of prototype can be used for user testingb. No, only fully interactive digital prototypes will give you useful datac. No, user testing is most effective when you use hand-drawn prototypes rather than ones created with graphic design softwared. No, you shouldn't use a prototype to conduct user testing | a. Yes, any type of prototype can be used for user testing
This example would most likely be an example of (choose the BEST response):a. Wireframesb. Hi-Fi Prototypec. Lo-Fi Prototyped. Yes, wireframes need to look like a completely finished product | b. Hi-Fi Prototype
从 Coursera 网络中提取的单个问题元素:
<div class="rc-FormPartsQuestion" aria-labelledby="WyLsh0XqEemmTA56ZlZejA-legend">
<div id="WyLsh0XqEemmTA56ZlZejA-legend" class="rc-FormPartsQuestion__row" data-test="legend">
<div class="rc-FormPartsQuestion__numberCell">
<h3 class=" css-6ecy9b" aria-hidden="true"><span>1</span>.</h3>
<span class="screenreader-only"><span>Question 1</span></span>
</div>
<div class="rc-FormPartsQuestion__contentCell" id="prompt-WyLsh0XqEemmTA56ZlZejA">
<div class=" css-4s48ix">
<div class="rc-CML" dir="auto">
<div data-testid="cml-viewer" class="css-1kgqbsw">
<p><span><span>Which of the following are among key tasks that were identified?</span></span></p>
<div>
<span><span></span></span>
<figure role="figure" contenteditable="false" tabindex="0"><img src="https://d3c33hcgiwev3.cloudfront.net/imageAssetProxy.v1/cFCt9EXqEemFOA6Hm29iNA_d8e5a45cdc401c6709f94f5621cbde15_Q3P1.png?expiry=1717891200000&hmac=MGuyCnj4ZzBRWWnYfgVhyQWTObvgL9xoRzBoWcJz9Ww" alt="" data-asset-id="cFCt9EXqEemFOA6Hm29iNA" class="cml-image-default undefined css-8qpjjf"></figure>
</div>
<p><span><span></span></span></p>
</div>
</div>
</div>
</div>
<div class="rc-FormPartsQuestion__pointsCell">
<div class="css-1mvreox"><span class=" css-6ecy9b"><span>1 point</span></span></div>
</div>
</div>
<div class="rc-FormPartsQuestion__row pii-hide" data-testid="form-part">
<div class="rc-FormPartsQuestion__numberCell"></div>
<div class="rc-FormPartsQuestion__contentCell">
<div class="rc-FormPartsMcq" role="group" aria-labelledby="WyLsh0XqEemmTA56ZlZejA-legend">
<div class="css-18k2uoc">
<div class="rc-Option">
<label class="_1oyudm1w cui-Checkbox cui-active">
<span class="_1e7axzp">
<svg class="_ufjrdd" aria-hidden="true" focusable="false" viewBox="0 0 24 24" role="img" aria-labelledby="RadioUncheckedc536e17c-9146-44c3-b9ce-79cb1c38e143 RadioUncheckedc536e17c-9146-44c3-b9ce-79cb1c38e143Desc" xmlns="http://www.w3.org/2000/svg" style="fill: rgb(102, 102, 102); height: 24px; width: 24px;">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" role="presentation"></path>
</svg>
</span>
<input class="_htmk7zm" name="WyLsh0XqEemmTA56ZlZejA" type="radio" tabindex="0" value="0.23852303460841262">
<span class="_bc4egv p-x-1s font-weight-normal">
<div class=" css-4s48ix">
<div class="rc-CML rc-Option__input-text" dir="auto">
<div data-testid="cml-viewer" class="css-1kgqbsw">
<p><span><span>Check map, Help, Activate function “Robbery”</span></span></p>
</div>
</div>
</div>
</span>
</label>
</div>
</div>
<div class="css-18k2uoc">
<div class="rc-Option">
<label class="_1oyudm1w cui-Checkbox cui-active">
<span class="_1e7axzp">
<svg class="_ufjrdd" aria-hidden="true" focusable="false" viewBox="0 0 24 24" role="img" aria-labelledby="RadioUncheckedea7f5299-8e99-41ea-f7fb-760595a3f422 RadioUncheckedea7f5299-8e99-41ea-f7fb-760595a3f422Desc" xmlns="http://www.w3.org/2000/svg" style="fill: rgb(102, 102, 102); height: 24px; width: 24px;">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" role="presentation"></path>
</svg>
</span>
<input class="_htmk7zm" name="WyLsh0XqEemmTA56ZlZejA" type="radio" tabindex="0" value="0.7149666518536051">
<span class="_bc4egv p-x-1s font-weight-normal">
<div class=" css-4s48ix">
<div class="rc-CML rc-Option__input-text" dir="auto">
<div data-testid="cml-viewer" class="css-1kgqbsw">
<p><span><span>Check map, Create a post, Log Out</span></span></p>
</div>
</div>
</div>
</span>
</label>
</div>
</div>
<div class="css-18k2uoc">
<div class="rc-Option">
<label class="_1oyudm1w cui-Checkbox cui-active">
<span class="_1e7axzp">
<svg class="_ufjrdd" aria-hidden="true" focusable="false" viewBox="0 0 24 24" role="img" aria-labelledby="RadioUncheckedd4ea4dc1-2638-4c68-8013-dd3c5a13d516 RadioUncheckedd4ea4dc1-2638-4c68-8013-dd3c5a13d516Desc" xmlns="http://www.w3.org/2000/svg" style="fill: rgb(102, 102, 102); height: 24px; width: 24px;">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" role="presentation"></path>
</svg>
</span>
<input class="_htmk7zm" name="WyLsh0XqEemmTA56ZlZejA" type="radio" tabindex="0" value="0.5749646870867715">
<span class="_bc4egv p-x-1s font-weight-normal">
<div class=" css-4s48ix">
<div class="rc-CML rc-Option__input-text" dir="auto">
<div data-testid="cml-viewer" class="css-1kgqbsw">
<p><span><span>Customize automatic functions, Help, Log Out</span></span></p>
</div>
</div>
</div>
</span>
</label>
</div>
</div>
<div class="css-18k2uoc">
<div class="rc-Option">
<label class="_1oyudm1w cui-Checkbox cui-active">
<span class="_1e7axzp">
<svg class="_ufjrdd" aria-hidden="true" focusable="false" viewBox="0 0 24 24" role="img" aria-labelledby="RadioUnchecked857f214b-e4df-40c8-e810-a3c5baaa0044 RadioUnchecked857f214b-e4df-40c8-e810-a3c5baaa0044Desc" xmlns="http://www.w3.org/2000/svg" style="fill: rgb(102, 102, 102); height: 24px; width: 24px;">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8z" role="presentation"></path>
</svg>
</span>
<input class="_htmk7zm" name="WyLsh0XqEemmTA56ZlZejA" type="radio" tabindex="0" value="0.603798411956308">
<span class="_bc4egv p-x-1s font-weight-normal">
<div class=" css-4s48ix">
<div class="rc-CML rc-Option__input-text" dir="auto">
<div data-testid="cml-viewer" class="css-1kgqbsw">
<p><span><span>Change security code, Create a post, Check recent events</span></span></p>
</div>
</div>
</div>
</span>
</label>
</div>
</div>
</div>
</div>
<div class="rc-FormPartsQuestion__pointsCell"></div>
</div>
</div>
我的问题是,它几乎可以解决问题,但对于上面的情况仍然不起作用,我做了一些控制台写入代码来检查:
// Worked
testQs-This example would most likely be an example of (choose the BEST response):-
sheetQs-This example would most likely be an example of (choose the BEST response):a. Wireframesb. Hi-Fi Prototypec. Lo-Fi Prototyped. Yes, wireframes need to look like a completely finished product-
sheetAns-b. Hi-Fi Prototype-
testAns-Wireframes-
testAns-Hi-Fi Prototype-
key-Hi-Fi Prototype- // If it run correctly, the key line should be logged to the console
testAns-Lo-Fi Prototype-
testAns-Yes, wireframes need to look like a completely finished product-
// Doesnt work
testQs-Which of the following are among key tasks that were identified?-
sheetQs-Which of the following are among key tasks that were identified?a. Check map, Help, Activate function "Robbery"b. Check map, Create a post, Log Outc. Customize automatic functions, Help, Log Outd. Change security code, Create a post, Check recent events-
sheetAns-a. Check map, Help, Activate function "Robbery"-
testAns-Check map, Help, Activate function "Robbery"-
testAns-Check map, Create a post, Log Out-
testAns-Customize automatic functions, Help, Log Out-
testAns-Change security code, Create a post, Check recent events-
看起来问题出在你的
html
:
<p><span><span>Check map, Help, Activate function “Robbery”</span></span></p>
“查看地图、帮助、激活“抢劫”功能” 不等于 “查看地图、帮助、激活“抢劫”功能”。
您的
html
中的引号与文本文件中的引号不同(“ vs ”)。更新 html(或文本文件),使两组引号匹配。